java Robot 类模拟键盘按键和鼠标点击(全局模拟)

Robot类用于为测试自动化、自运行演示程序和其他需要控制鼠标和键盘的应用程序生成本机系统输入事件。Robot 的主要目的是便于 Java 平台实现自动测试。

    使用该类生成输入事件与将事件发送到 AWT 事件队列或 AWT 组件的区别在于:事件是在平台的本机输入队列中生成的。例如,Robot.mouseMove 将实际移动鼠标光标,而不是只生成鼠标移动事件。

    我们可以利用这个类写出好多有意思的东西,下面我将详细的这个类的用法。

    Robot这个类中的方法并不是很多,常用的有以下几个:

    void delay (int ms)  睡眠指定的时间(类似于线程中sleep)

    void keyPress(int keycode) 按下指定的键

    void keyRelease(int keycode) 释放指定的键

    void mouseMove(int x,int y) 将鼠标移动到给定的屏幕坐标上

    void mousePress(int buttons) 按下一个或多个鼠标按键

    void mouseRelease(int buttons) 释放一个活多个鼠标按键

    void mouseWheel(int wheelAmt) 滚动鼠标滑轮

下面看一个自动实现HelloWorld的例子:

  1. public class Main {  
  2.     private static Robot robot;  
  3.   
  4.     public static void main(String[] args) {  
  5.         try {  
  6.             robot = new Robot();//创建Robot对象  
  7.         } catch (Exception e) {  
  8.             // TODO Auto-generated catch block  
  9.             e.printStackTrace();  
  10.         }  
  11.         //这里是按下和释放alt键,这个键的功能是调用菜单  
  12.         robot.keyPress(KeyEvent.VK_ALT);  
  13.         robot.keyRelease(KeyEvent.VK_ALT);  
  14.         robot.delay(500);//延迟500ms  
  15.         //这里是按下和释放回车键,用于确定是file选项  
  16.         robot.keyPress(KeyEvent.VK_ENTER);  
  17.         robot.keyRelease(KeyEvent.VK_ENTER);  
  18.         robot.delay(500);  
  19.         //选择New  
  20.         robot.keyPress(KeyEvent.VK_ENTER);  
  21.         robot.keyRelease(KeyEvent.VK_ENTER);  
  22.         robot.delay(500);  
  23.         //选择Java Project  
  24.         robot.keyPress(KeyEvent.VK_ENTER);  
  25.         robot.keyRelease(KeyEvent.VK_ENTER);  
  26.         robot.delay(500);  
  27.           
  28.         //输入字母a,即工程名  
  29.         robot.keyPress(KeyEvent.VK_A);  
  30.         robot.keyRelease(KeyEvent.VK_A);  
  31.         robot.delay(500);  
  32.           
  33.         //回车确定创建工程  
  34.         robot.keyPress(KeyEvent.VK_ENTER);        
  35.         robot.keyRelease(KeyEvent.VK_ENTER);          
  36.         robot.delay(500);         
  37.         //将鼠标抵用到刚刚建立的工程上面,这个坐标是作者在自己的电脑上实验获得的,在不同的电脑上可能
  38. 不一样,可以用  
  39.         //Point point = MouseInfo.getPointerInfo().getLocation();  
  40.         //System.out.println(point);来打印当前鼠标的坐标,从而找到对的位置  
  41.         robot.mouseMove(43136);  
  42.         //按下和释放鼠标左键,选定工程  
  43.         robot.mousePress(KeyEvent.BUTTON1_MASK);  
  44.         robot.mouseRelease(KeyEvent.BUTTON1_MASK);  
  45.   
  46.         //alt键选择菜单并调出newClass的选项  
  47.         robot.keyPress(KeyEvent.VK_ALT);  
  48.         robot.keyRelease(KeyEvent.VK_ALT);  
  49.         robot.delay(500);  
  50.         robot.keyPress(KeyEvent.VK_ENTER);  
  51.         robot.keyRelease(KeyEvent.VK_ENTER);  
  52.         robot.delay(500);  
  53.         robot.keyPress(KeyEvent.VK_ENTER);  
  54.         robot.keyRelease(KeyEvent.VK_ENTER);  
  55.         robot.delay(500);  
  56.         robot.keyPress(KeyEvent.VK_DOWN);  
  57.         robot.keyRelease(KeyEvent.VK_DOWN);  
  58.         robot.delay(500);  
  59.         robot.keyPress(KeyEvent.VK_DOWN);  
  60.         robot.keyRelease(KeyEvent.VK_DOWN);  
  61.         robot.delay(500);  
  62.         robot.keyPress(KeyEvent.VK_DOWN);  
  63.         robot.keyRelease(KeyEvent.VK_DOWN);  
  64.         robot.delay(500);  
  65.         robot.keyPress(KeyEvent.VK_ENTER);  
  66.         robot.keyRelease(KeyEvent.VK_ENTER);  
  67.         robot.delay(500);  
  68.         //下面换回小写  
  69.         //切换成大写  
  70.         robot.keyPress(KeyEvent.VK_CAPS_LOCK);  
  71.         robot.keyRelease(KeyEvent.VK_CAPS_LOCK);  
  72.         robot.delay(500);  
  73.         robot.keyPress(KeyEvent.VK_H);  
  74.         robot.keyRelease(KeyEvent.VK_H);  
  75.         robot.delay(200);  
  76.         //换回小写  
  77.         robot.keyPress(KeyEvent.VK_CAPS_LOCK);  
  78.         robot.keyRelease(KeyEvent.VK_CAPS_LOCK);  
  79.         robot.delay(200);  
  80.         robot.keyPress(KeyEvent.VK_E);  
  81.         robot.keyRelease(KeyEvent.VK_E);  
  82.         robot.delay(200);  
  83.         robot.keyPress(KeyEvent.VK_L);  
  84.         robot.keyRelease(KeyEvent.VK_L);  
  85.         robot.delay(200);  
  86.         robot.keyPress(KeyEvent.VK_L);  
  87.         robot.keyRelease(KeyEvent.VK_L);  
  88.         robot.delay(200);  
  89.         robot.keyPress(KeyEvent.VK_O);  
  90.         robot.keyRelease(KeyEvent.VK_O);  
  91.         robot.delay(200);  
  92.         robot.keyPress(KeyEvent.VK_CAPS_LOCK);  
  93.         robot.keyRelease(KeyEvent.VK_CAPS_LOCK);  
  94.         robot.delay(200);  
  95.         robot.keyPress(KeyEvent.VK_W);  
  96.         robot.keyRelease(KeyEvent.VK_W);  
  97.         robot.delay(200);  
  98.         robot.keyPress(KeyEvent.VK_CAPS_LOCK);  
  99.         robot.keyRelease(KeyEvent.VK_CAPS_LOCK);  
  100.         robot.delay(200);  
  101.         robot.keyPress(KeyEvent.VK_O);  
  102.         robot.keyRelease(KeyEvent.VK_O);  
  103.         robot.delay(200);  
  104.         robot.keyPress(KeyEvent.VK_R);  
  105.         robot.keyRelease(KeyEvent.VK_R);  
  106.         robot.delay(200);  
  107.         robot.keyPress(KeyEvent.VK_L);  
  108.         robot.keyRelease(KeyEvent.VK_L);  
  109.         robot.delay(200);  
  110.         robot.keyPress(KeyEvent.VK_D);  
  111.         robot.keyRelease(KeyEvent.VK_D);  
  112.         robot.delay(200);  
  113.           
  114.         //移动鼠标选择自动穿件main函数  
  115.         robot.mouseMove(533448);  
  116.         robot.mousePress(KeyEvent.BUTTON1_MASK);  
  117.         robot.mouseRelease(KeyEvent.BUTTON1_MASK);  
  118.         robot.delay(500);  
  119.         //回车创建完成  
  120.         robot.keyPress(KeyEvent.VK_ENTER);  
  121.         robot.keyRelease(KeyEvent.VK_ENTER);  
  122.         robot.delay(500);  
  123.         //移动光标到输入代码的行上  
  124.         robot.keyPress(KeyEvent.VK_DOWN);  
  125.         robot.keyRelease(KeyEvent.VK_DOWN);  
  126.         robot.delay(500);  
  127.         robot.keyPress(KeyEvent.VK_DOWN);  
  128.         robot.keyRelease(KeyEvent.VK_DOWN);  
  129.         robot.delay(500);  
  130.         robot.keyPress(KeyEvent.VK_DOWN);  
  131.         robot.keyRelease(KeyEvent.VK_DOWN);  
  132.         robot.delay(500);  
  133.         robot.keyPress(KeyEvent.VK_DOWN);  
  134.         robot.keyRelease(KeyEvent.VK_DOWN);  
  135.         robot.delay(500);  
  136.         robot.keyPress(KeyEvent.VK_DOWN);  
  137.         robot.keyRelease(KeyEvent.VK_DOWN);  
  138.         robot.delay(500);  
  139.         robot.keyPress(KeyEvent.VK_DOWN);  
  140.         robot.keyRelease(KeyEvent.VK_DOWN);  
  141.         robot.delay(500);  
  142.   
  143.         //输入syso并用alt+/快捷键补全  
  144.         robot.keyPress(KeyEvent.VK_S);  
  145.         robot.keyRelease(KeyEvent.VK_S);  
  146.         robot.delay(200);  
  147.         robot.keyPress(KeyEvent.VK_Y);  
  148.         robot.keyRelease(KeyEvent.VK_Y);  
  149.         robot.delay(200);  
  150.         robot.keyPress(KeyEvent.VK_S);  
  151.         robot.keyRelease(KeyEvent.VK_S);  
  152.         robot.delay(200);  
  153.         robot.keyPress(KeyEvent.VK_O);  
  154.         robot.keyRelease(KeyEvent.VK_O);  
  155.         robot.delay(200);  
  156.         robot.keyPress(KeyEvent.VK_ALT);  
  157.         robot.keyPress(KeyEvent.VK_SLASH);  
  158.         robot.keyRelease(KeyEvent.VK_ALT);  
  159.         robot.keyRelease(KeyEvent.VK_SLASH);  
  160.         robot.delay(500);  
  161.   
  162.         //输入双引号  
  163.         robot.keyPress(KeyEvent.VK_SHIFT);  
  164.         robot.keyPress(KeyEvent.VK_QUOTE);  
  165.         robot.keyRelease(KeyEvent.VK_SHIFT);  
  166.         robot.keyRelease(KeyEvent.VK_QUOTE);  
  167.   
  168.         //输入HelloWorld  
  169.         robot.keyPress(KeyEvent.VK_CAPS_LOCK);  
  170.         robot.keyRelease(KeyEvent.VK_CAPS_LOCK);  
  171.         robot.delay(500);  
  172.         robot.keyPress(KeyEvent.VK_H);  
  173.         robot.keyRelease(KeyEvent.VK_H);  
  174.         robot.delay(200);  
  175.         robot.keyPress(KeyEvent.VK_CAPS_LOCK);  
  176.         robot.keyRelease(KeyEvent.VK_CAPS_LOCK);  
  177.         robot.delay(200);  
  178.         robot.keyPress(KeyEvent.VK_E);  
  179.         robot.keyRelease(KeyEvent.VK_E);  
  180.         robot.delay(200);  
  181.         robot.keyPress(KeyEvent.VK_L);  
  182.         robot.keyRelease(KeyEvent.VK_L);  
  183.         robot.delay(200);  
  184.         robot.keyPress(KeyEvent.VK_L);  
  185.         robot.keyRelease(KeyEvent.VK_L);  
  186.         robot.delay(200);  
  187.         robot.keyPress(KeyEvent.VK_O);  
  188.         robot.keyRelease(KeyEvent.VK_O);  
  189.         robot.delay(200);  
  190.         robot.keyPress(KeyEvent.VK_CAPS_LOCK);  
  191.         robot.keyRelease(KeyEvent.VK_CAPS_LOCK);  
  192.         robot.delay(200);  
  193.         robot.keyPress(KeyEvent.VK_W);  
  194.         robot.keyRelease(KeyEvent.VK_W);  
  195.         robot.delay(200);  
  196.         robot.keyPress(KeyEvent.VK_CAPS_LOCK);  
  197.         robot.keyRelease(KeyEvent.VK_CAPS_LOCK);  
  198.         robot.delay(200);  
  199.         robot.keyPress(KeyEvent.VK_O);  
  200.         robot.keyRelease(KeyEvent.VK_O);  
  201.         robot.delay(200);  
  202.         robot.keyPress(KeyEvent.VK_R);  
  203.         robot.keyRelease(KeyEvent.VK_R);  
  204.         robot.delay(200);  
  205.         robot.keyPress(KeyEvent.VK_L);  
  206.         robot.keyRelease(KeyEvent.VK_L);  
  207.         robot.delay(200);  
  208.         robot.keyPress(KeyEvent.VK_D);  
  209.         robot.keyRelease(KeyEvent.VK_D);  
  210.         robot.delay(200);  
  211.   
  212.         //ctrl+shift+F 格式化  
  213.         robot.keyPress(KeyEvent.VK_SHIFT);  
  214.         robot.keyPress(KeyEvent.VK_CONTROL);  
  215.         robot.keyPress(KeyEvent.VK_F);  
  216.         robot.keyRelease(KeyEvent.VK_SHIFT);  
  217.         robot.keyRelease(KeyEvent.VK_CONTROL);  
  218.         robot.keyRelease(KeyEvent.VK_F);  
  219.           
  220.         //ctrl+S保存  
  221.         robot.keyPress(KeyEvent.VK_CONTROL);  
  222.         robot.keyPress(KeyEvent.VK_S);  
  223.         robot.keyRelease(KeyEvent.VK_S);  
  224.         robot.keyRelease(KeyEvent.VK_CONTROL);  
  225.           
  226.         //F11运行  
  227.         robot.keyPress(KeyEvent.VK_F11);  
  228.         robot.keyRelease(KeyEvent.VK_F11);        
  229.           
  230.     }     
  231. }

你可能感兴趣的:(JAVA)