java里面有意思的robot

Share

java里面,经常看api的人不难发现,在java.awt包下,有个非常有意思也有用的类Robot!它可以模拟键盘和鼠标的一切动作,看看
原api
public class Robot
extends Object

This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations.

Using the class to generate input events differs from posting events to the AWT event queue or AWT components in that the events are generated in the platform’s native input queue. For example, Robot.mouseMove will actually move the mouse cursor instead of just generating mouse move events.

Note that some platforms require special privileges or extensions to access low-level input control. If the current platform configuration does not allow input control, an AWTException will be thrown when trying to construct Robot objects. For example, X-Window systems will throw the exception if the XTEST 2.2 standard extension is not supported (or not enabled) by the X server.

Applications that use Robot for purposes other than self-testing should handle these error conditions gracefully.

下面是写的一个DEMO:

/**					
 * @author    leekooqi					
 * @company   leemenz (C) copyright					
 * @time      Sep 24, 2007  9:16:24 AM					
 * @version   1.0.0.0					
 * @package   com.icnote.test					
 */					
package   com.icnote.test					
					
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) {					
robot.keyPress(KeyEvent.VK_ALT);					
robot.keyPress(KeyEvent.VK_F4);					
robot.keyRelease(KeyEvent.VK_ALT);					
robot.keyRelease(KeyEvent.VK_F4);					
robot.keyPress(KeyEvent.VK_N);					
robot.keyRelease(KeyEvent.VK_N);					
}					
public static void writeCharacter(){					
try {					
Robot robot = new Robot();					
Runtime.getRuntime().exec("notepad");					
robot.delay(3000);//延迟3000ms,等你的notepad打开					
pressKeyWithShift(robot, KeyEvent.VK_H);					
robot.delay(500);//延迟500ms,有Typing的效果 :)					
pressKey(robot, KeyEvent.VK_I);					
robot.delay(500);					
pressKey(robot, KeyEvent.VK_SPACE);					
robot.delay(500);					
pressKeyWithShift(robot, KeyEvent.VK_I);					
robot.delay(500);					
pressKey(robot, KeyEvent.VK_SPACE);					
robot.delay(500);					
pressKey(robot, KeyEvent.VK_A);					
robot.delay(500);					
pressKey(robot, KeyEvent.VK_M);					
robot.delay(500);					
pressKey(robot, KeyEvent.VK_SPACE);					
robot.delay(500);					
pressKey(robot, KeyEvent.VK_T);					
robot.delay(500);					
pressKey(robot, KeyEvent.VK_H);					
robot.delay(500);					
pressKey(robot, KeyEvent.VK_E);					
robot.delay(500);					
pressKey(robot, KeyEvent.VK_SPACE);					
robot.delay(500);					
pressKey(robot, KeyEvent.VK_J);					
robot.delay(500);					
pressKey(robot, KeyEvent.VK_A);					
robot.delay(500);					
pressKey(robot, KeyEvent.VK_V);					
robot.delay(500);					
pressKey(robot, KeyEvent.VK_A);					
robot.delay(500);					
pressKey(robot, KeyEvent.VK_SPACE);					
robot.delay(500);					
pressKey(robot, KeyEvent.VK_R);					
robot.delay(500);					
pressKey(robot, KeyEvent.VK_O);					
robot.delay(500);					
pressKey(robot, KeyEvent.VK_B);					
robot.delay(500);					
pressKey(robot, KeyEvent.VK_O);					
robot.delay(500);					
pressKey(robot, KeyEvent.VK_T);					
robot.delay(1000);					
pressKey(robot, KeyEvent.VK_SPACE);					
robot.delay(500);					
robot.keyPress(KeyEvent.VK_SHIFT);					
robot.keyPress(KeyEvent.VK_1);					
robot.keyRelease(KeyEvent.VK_1);					
robot.keyRelease(KeyEvent.VK_SHIFT);					
robot.delay(500);					
robot.keyPress(KeyEvent.VK_SHIFT);					
robot.keyPress(KeyEvent.VK_1);					
robot.keyRelease(KeyEvent.VK_1);					
robot.keyRelease(KeyEvent.VK_SHIFT);					
robot.delay(500);					
pressKey(robot, KeyEvent.VK_ENTER);					
closeApplication(robot);					
} catch (AWTException e) {					
e.printStackTrace();					
}catch(Exception e2) {					
e2.printStackTrace();					
}					
}					
public static void main(String[] args) throws IOException {					
  writeCharacter();					
}					
}					

哈哈。。。慢慢研究吧。。。