How to use Robot class in Java

Java.awt.Robot class for controlling the keyboard and mouse. Once you have this control, you can do any kind of operation with the mouse and keyboard through your Java code. This class is typically used in automated testing. Sample Code first surface Robot class example will show you how to handle keyboard events if you run this code, and open the notepad, you will see the HI CAOER in the notepad give this a try...
Import java.awt.AWTException;
Import the java.awt. Robot;
Import java.awt.event.KeyEvent;
public class RobotExp {
public static void main (String [] args) {
the try {
Robot Robot Robot new new = ();
// definition of delay of five seconds so that you open the notepad ha
@ Robot began to write
robot.delay (5000);
robot.keyPress (KeyEvent.VK_H);
robot.keyPress (KeyEvent.VK_I);
robot.keyPress (KeyEvent.VK_SPACE);
robot.keyPress (KeyEvent.VK_C);
robot.keyPress ( KeyEvent.VK_A);
robot.keyPress (KeyEvent.VK_O);
robot.keyPress(KeyEvent.VK_E);
robot.keyPress(KeyEvent.VK_R);
} catch (AWTException e) {
e.printStackTrace();
}
}
}
网友完善了以上代码:
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
public class RobotExp {
public static void pressKey(Robot robot, int keyvalue) {
robot.keyPress(keyvalue);
robot.keyRelease(keyvalue);
}
public static void pressKeyWithShift(Robot robot, int keyvalue) {
robot.keyPress(KeyEvent.VK_SHIFT);
robot.keyPress(keyvalue);
robot.keyRelease(keyvalue);
robot.keyRelease(KeyEvent.VK_SHIFT);
}
public static void closeApplication(Robot robot) {
// pressKey(robot, KeyEvent.VK_ALT);
// pressKey(robot, KeyEvent.VK_F4);
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_F4);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_F4);
//for linux.
// robot.keyPress(KeyEvent.VK_ALT);
// robot.keyPress(KeyEvent.VK_W);
// robot.keyRelease(KeyEvent.VK_ALT);
// robot.keyRelease(KeyEvent.VK_W);
robot.keyPress(KeyEvent.VK_N);
robot.keyRelease(KeyEvent.VK_N);
}
public static void main(String[] args) throws IOException {
{the try
Robot Robot Robot new new = ();
. Runtime.getRuntime () Exec ( "Notepad");
. // the For Linux
//Runtime.getRuntime().exec("gedit ");
// definition of delay of five seconds so you open notepad ha
// Robot start writing
robot.delay (3000);
for (int I = 0; I <100; I ++) {
pressKeyWithShift (Robot, KeyEvent.VK_H);
pressKey (Robot, KeyEvent.VK_I);
pressKey (Robot, KeyEvent.VK_SPACE);
// pressKeyWithShift (Robot, KeyEvent.VK_H);
pressKeyWithShift (Robot, KeyEvent.VK_I);
pressKey (Robot, KeyEvent.VK_SPACE);
pressKey (Robot, KeyEvent.VK_A);
pressKey (Robot, KeyEvent.VK_M);
pressKey (Robot, KeyEvent.VK_SPACE);
pressKey (Robot, KeyEvent.VK_T);
pressKey (robot KeyEvent.VK_H);
pressKey (robot KeyEvent.VK_E);
pressKey (robot KeyEvent.VK_SPACE);
pressKey (robot KeyEvent.VK_J);
pressKey (robot KeyEvent.VK_A);
pressKey (robot KeyEvent.VK_V);
pressKey (robot KeyEvent.VK_A);
pressKey (robot KeyEvent.VK_SPACE);
pressKey (robot KeyEvent.VK_R);
pressKey (robot KeyEvent.VK_O);
pressKey (robot KeyEvent.VK_B);
pressKey (robot KeyEvent.VK_O);
pressKey (robot KeyEvent.VK_T);
// VK_ENTER
pressKey (robot KeyEvent.VK_ENTER);
// pressKey (robot KeyEvent.);
}
Close application (robot);
//robot.keyPress(KeyEvent.VK_SPACE);
The catch} (AWTException E) {
e.printStackTrace ();
}
}
}
Author: Lu finishing Source: China Network Management Alliance

Reproduced in: https: //www.cnblogs.com/521taobao/archive/2012/03/17/2402490.html

Guess you like

Origin blog.csdn.net/weixin_34184158/article/details/93355958