Thursday, February 26, 2009

Robot in Java

java.awt.Robot can be used to automate Keyboard,Mouse and many other. Here is a small
example of a keyboard operation the Robot object can do. 

RobotTest.java

import java.awt.*;
import java.awt.event.*;

/*
 * @author Shazin Sadakath
 * 
*/

public class RobotTest{
public static void main(String[] args){
try{
Robot robot = new Robot();
robot.delay(500);
robot.keyPress(KeyEvent.VK_C);
robot.keyPress(KeyEvent.VK_O);
robot.keyPress(KeyEvent.VK_D);
robot.keyPress(KeyEvent.VK_E);
robot.keyPress(KeyEvent.VK_R);
robot.keyPress(KeyEvent.VK_P);
robot.keyPress(KeyEvent.VK_A);
robot.keyPress(KeyEvent.VK_S);
robot.keyPress(KeyEvent.VK_S);
robot.keyPress(KeyEvent.VK_I);
robot.keyPress(KeyEvent.VK_O);
robot.keyPress(KeyEvent.VK_N);
}catch(Exception e){
System.err.println(e.getMessage());
}
}
}

open up a notepad and then run the RobotTest

$ java RobotTest

and then click on the opened up notepad.

No comments:

Post a Comment