java里面有意思的robot

java里面,经常看api的人不难发现,在java.awt包下,有个非常有意思也有用的类Robot!它可以模拟键盘和鼠标的一切动作,看看 huachi typying
     原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:
  1. /**  
  2.  * @author      
  3.  * @company   leemenz (C) copyright  
  4.  * @time      Sep 24, 2007  9:16:24 AM  
  5.  * @version   1.0.0.0  
  6.  * @package   com.icnote.test  
  7.  */  
  8. package   com.icnote.test   
  9.   
  10. import java.awt.AWTException;   
  11. import java.awt.Robot;   
  12. import java.awt.event.KeyEvent;   
  13. import java.io.IOException;   
  14.   
  15. public class RobotExp {   
  16. public static void pressKey(Robot robot, int keyvalue) {   
  17. robot.keyPress(keyvalue);   
  18. robot.keyRelease(keyvalue);   
  19. }   
  20. public static void pressKeyWithShift(Robot robot, int keyvalue) {   
  21. robot.keyPress(KeyEvent.VK_SHIFT);   //先按下   
  22. robot.keyPress(keyvalue);   
  23. robot.keyRelease(keyvalue);   
  24. robot.keyRelease(KeyEvent.VK_SHIFT);  //再松开(顺序可不能错啊!)   
  25. }   
  26. public static void closeApplication(Robot robot) {   
  27. robot.keyPress(KeyEvent.VK_ALT);   
  28. robot.keyPress(KeyEvent.VK_F4);   
  29. robot.keyRelease(KeyEvent.VK_ALT);   
  30. robot.keyRelease(KeyEvent.VK_F4);   
  31. robot.keyPress(KeyEvent.VK_N);   
  32. robot.keyRelease(KeyEvent.VK_N);   
  33. }   
  34. public static void writeCharacter(){   
  35. try {   
  36. Robot robot = new Robot();   
  37. Runtime.getRuntime().exec("notepad");   
  38. robot.delay(3000);//延迟3000ms,等你的notepad打开   
  39. pressKeyWithShift(robot, KeyEvent.VK_H);   
  40. robot.delay(500);//延迟500ms,有Typing的效果 :)   
  41. pressKey(robot, KeyEvent.VK_I);   
  42. robot.delay(500);   
  43. pressKey(robot, KeyEvent.VK_SPACE);   
  44. robot.delay(500);   
  45. pressKeyWithShift(robot, KeyEvent.VK_I);   
  46. robot.delay(500);   
  47. pressKey(robot, KeyEvent.VK_SPACE);   
  48. robot.delay(500);   
  49. pressKey(robot, KeyEvent.VK_A);   
  50. robot.delay(500);   
  51. pressKey(robot, KeyEvent.VK_M);   
  52. robot.delay(500);   
  53. pressKey(robot, KeyEvent.VK_SPACE);   
  54. robot.delay(500);   
  55. pressKey(robot, KeyEvent.VK_T);   
  56. robot.delay(500);   
  57. pressKey(robot, KeyEvent.VK_H);   
  58. robot.delay(500);   
  59. pressKey(robot, KeyEvent.VK_E);   
  60. robot.delay(500);   
  61. pressKey(robot, KeyEvent.VK_SPACE);   
  62. robot.delay(500);   
  63. pressKey(robot, KeyEvent.VK_J);   
  64. robot.delay(500);   
  65. pressKey(robot, KeyEvent.VK_A);   
  66. robot.delay(500);   
  67. pressKey(robot, KeyEvent.VK_V);   
  68. robot.delay(500);   
  69. pressKey(robot, KeyEvent.VK_A);   
  70. robot.delay(500);   
  71. pressKey(robot, KeyEvent.VK_SPACE);   
  72. robot.delay(500);   
  73. pressKey(robot, KeyEvent.VK_R);   
  74. robot.delay(500);   
  75. pressKey(robot, KeyEvent.VK_O);   
  76. robot.delay(500);   
  77. pressKey(robot, KeyEvent.VK_B);   
  78. robot.delay(500);   
  79. pressKey(robot, KeyEvent.VK_O);   
  80. robot.delay(500);   
  81. pressKey(robot, KeyEvent.VK_T);   
  82. robot.delay(1000);   
  83. pressKey(robot, KeyEvent.VK_SPACE);   
  84. robot.delay(500);   
  85. robot.keyPress(KeyEvent.VK_SHIFT);   
  86. robot.keyPress(KeyEvent.VK_1);   
  87. robot.keyRelease(KeyEvent.VK_1);   
  88. robot.keyRelease(KeyEvent.VK_SHIFT);   
  89. robot.delay(500);   
  90. robot.keyPress(KeyEvent.VK_SHIFT);   
  91. robot.keyPress(KeyEvent.VK_1);   
  92. robot.keyRelease(KeyEvent.VK_1);   
  93. robot.keyRelease(KeyEvent.VK_SHIFT);   
  94. robot.delay(500);   
  95. pressKey(robot, KeyEvent.VK_ENTER);   
  96. closeApplication(robot);   
  97. catch (AWTException e) {   
  98. e.printStackTrace();   
  99. }catch(Exception e2) {   
  100. e2.printStackTrace();   
  101. }   
  102. }   
  103. public static void main(String[] args) throws IOException {   
  104.   writeCharacter();   
  105. }   
  106. }   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值