자바로 강제키입력 (java java.awt.Robot)

자바로 강제키입력

 

실행하면 강제로 폴더를 새로 열어서 google.com을 키입력하고 엔터키를 누르는 매크로.

 

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;

public class Main  {
 static Robot robot;
 public static void main(String[] args) throws Exception{

   //로봇을 생성
   robot=new Robot();
   
   WinE();
   robot.delay(1000);
   System.out.println(“a”);
   AltD();
   
   keyPress(“google.com”);

 }
 public static void keyPress(String str) throws Exception{
  for(int i=0;i<str.length();i++){
   
   String mykey=str.substring(i,i+1);
   int newkey=mykey.charAt(0);
   
   if(mykey.equals(“:”)){
    //Colon();
   }
   else if(mykey.equals(“/”)){
    Slash();
   }
   else if(mykey.equals(“.”)){
    Dot();
   }
   else{
    PressWhat(newkey);
   }
   
   robot.delay(200);
  }
  Enter();
 }
 public static void PressWhat(int nkey) throws Exception{
  robot.keyPress(nkey-32);
  robot.keyRelease(nkey-32);
 }
 public static void Enter() throws Exception{
  robot.keyPress(KeyEvent.VK_ENTER);
  robot.keyRelease(KeyEvent.VK_ENTER); //97을 넣으면 숫자1이 나옴
 }
 public static void Dot() throws Exception{
  robot.keyPress(46);
  robot.keyRelease(46);
 }
 public static void Slash() throws Exception{
  robot.keyPress(KeyEvent.VK_SLASH);
  robot.keyRelease(KeyEvent.VK_SLASH);
 }
 
 public static void WinE() throws Exception{
  robot.keyPress(KeyEvent.VK_WINDOWS);
  robot.keyPress(KeyEvent.VK_E);
  robot.keyRelease(KeyEvent.VK_E);
  robot.keyRelease(KeyEvent.VK_WINDOWS);
  robot.delay(400);
  System.out.println(“윈도우키+E”);
 }
 
 public static void AltD() throws Exception{
  robot.keyPress(KeyEvent.VK_ALT);
  robot.keyPress(KeyEvent.VK_D);
  robot.keyRelease(KeyEvent.VK_D);
  robot.keyRelease(KeyEvent.VK_ALT);
  robot.delay(200);
  System.out.println(“알트+D”);
 }
}