View RSS Feed

Java AWT

How to use Robot class in Java?

Rate this Entry
by , 03-04-2012 at 10:43 AM (1200 Views)
For controlling the keyboard or mouse, class java.awt.Robot will be used. Any kind of operation regarding mouse or keyboard can be performed once one has the control over mouse & keyboard. Usually such class type is used while doing test automation.

Robot class which handles the keyboard events will be shown up by using this code. If this code is run, a notepad will be opened to write down “HI MY FRIEND”.
Java Code:
import java.awt.AWTException; 
import java.awt.Robot; 
import java.awt.event.KeyEvent; 
public class RobotExp { 
public static void main(String[] args) { 
try { 
Robot robot = new Robot(); 
// Creates the delay of 5 sec so that you can open notepad before 
// Robot start writting 
robot.delay(5000); 
robot.keyPress(KeyEvent.VK_H); 
robot.keyPress(KeyEvent.VK_I); 
robot.keyPress(KeyEvent.VK_SPACE); 
robot.keyPress(KeyEvent.VK_M); 
robot.keyPress(KeyEvent.VK_Y); 
robot.keyPress(KeyEvent.VK_SPACE); 
robot.keyPress(KeyEvent.VK_F); 
robot.keyPress(KeyEvent.VK_R); 
robot.keyPress(KeyEvent.VK_I); 
robot.keyPress(KeyEvent.VK_E); 
robot.keyPress(KeyEvent.VK_N); 
robot.keyPress(KeyEvent.VK_D); 
} catch (AWTException e) { 
e.printStackTrace(); 
} 
} 
}

Submit "How to use Robot class in Java?" to Facebook Submit "How to use Robot class in Java?" to Digg Submit "How to use Robot class in Java?" to del.icio.us Submit "How to use Robot class in Java?" to StumbleUpon Submit "How to use Robot class in Java?" to Google

Categories
Robot

Comments