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的例子:

package autoHelloWorld;

import java.awt.Robot;
import java.awt.event.KeyEvent;

public class Main {
 private static Robot robot;

public static void main(String[] args) {
  try {
   robot = new Robot();//创建Robot对象
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  //这里是按下和释放alt键,这个键的功能是调用菜单
  robot.keyPress(KeyEvent.VK_ALT);
  robot.keyRelease(KeyEvent.VK_ALT);
  robot.delay(500);//延迟500ms
  //这里是按下和释放回车键,用于确定是file选项
  robot.keyPress(KeyEvent.VK_ENTER);
  robot.keyRelease(KeyEvent.VK_ENTER);
  robot.delay(500);
  //选择New
  robot.keyPress(KeyEvent.VK_ENTER);
  robot.keyRelease(KeyEvent.VK_ENTER);
  robot.delay(500);
  //选择Java Project
  robot.keyPress(KeyEvent.VK_ENTER);
  robot.keyRelease(KeyEvent.VK_ENTER);
  robot.delay(500);
 
  //输入字母a,即工程名
  robot.keyPress(KeyEvent.VK_A);
  robot.keyRelease(KeyEvent.VK_A);
  robot.delay(500);
 
  //回车确定创建工程
  robot.keyPress(KeyEvent.VK_ENTER); 
  robot.keyRelease(KeyEvent.VK_ENTER); 
  robot.delay(500); 
  //将鼠标抵用到刚刚建立的工程上面,这个坐标是作者在自己的电脑上实验获得的,在不同的电脑上可能不一样,可以用
  //Point point = MouseInfo.getPointerInfo().getLocation();
  //System.out.println(point);来打印当前鼠标的坐标,从而找到对的位置
  robot.mouseMove(43, 136);
  //按下和释放鼠标左键,选定工程
  robot.mousePress(KeyEvent.BUTTON1_MASK);
  robot.mouseRelease(KeyEvent.BUTTON1_MASK);

//alt键选择菜单并调出newClass的选项
  robot.keyPress(KeyEvent.VK_ALT);
  robot.keyRelease(KeyEvent.VK_ALT);
  robot.delay(500);
  robot.keyPress(KeyEvent.VK_ENTER);
  robot.keyRelease(KeyEvent.VK_ENTER);
  robot.delay(500);
  robot.keyPress(KeyEvent.VK_ENTER);
  robot.keyRelease(KeyEvent.VK_ENTER);
  robot.delay(500);
  robot.keyPress(KeyEvent.VK_DOWN);
  robot.keyRelease(KeyEvent.VK_DOWN);
  robot.delay(500);
  robot.keyPress(KeyEvent.VK_DOWN);
  robot.keyRelease(KeyEvent.VK_DOWN);
  robot.delay(500);
  robot.keyPress(KeyEvent.VK_DOWN);
  robot.keyRelease(KeyEvent.VK_DOWN);
  robot.delay(500);
  robot.keyPress(KeyEvent.VK_ENTER);
  robot.keyRelease(KeyEvent.VK_ENTER);
  robot.delay(500);
  //下面换回小写
  //切换成大写
  robot.keyPress(KeyEvent.VK_CAPS_LOCK);
  robot.keyRelease(KeyEvent.VK_CAPS_LOCK);
  robot.delay(500);
  robot.keyPress(KeyEvent.VK_H);
  robot.keyRelease(KeyEvent.VK_H);
  robot.delay(200);
  //换回小写
  robot.keyPress(KeyEvent.VK_CAPS_LOCK);
  robot.keyRelease(KeyEvent.VK_CAPS_LOCK);
  robot.delay(200);
  robot.keyPress(KeyEvent.VK_E);
  robot.keyRelease(KeyEvent.VK_E);
  robot.delay(200);
  robot.keyPress(KeyEvent.VK_L);
  robot.keyRelease(KeyEvent.VK_L);
  robot.delay(200);
  robot.keyPress(KeyEvent.VK_L);
  robot.keyRelease(KeyEvent.VK_L);
  robot.delay(200);
  robot.keyPress(KeyEvent.VK_O);
  robot.keyRelease(KeyEvent.VK_O);
  robot.delay(200);
  robot.keyPress(KeyEvent.VK_CAPS_LOCK);
  robot.keyRelease(KeyEvent.VK_CAPS_LOCK);
  robot.delay(200);
  robot.keyPress(KeyEvent.VK_W);
  robot.keyRelease(KeyEvent.VK_W);
  robot.delay(200);
  robot.keyPress(KeyEvent.VK_CAPS_LOCK);
  robot.keyRelease(KeyEvent.VK_CAPS_LOCK);
  robot.delay(200);
  robot.keyPress(KeyEvent.VK_O);
  robot.keyRelease(KeyEvent.VK_O);
  robot.delay(200);
  robot.keyPress(KeyEvent.VK_R);
  robot.keyRelease(KeyEvent.VK_R);
  robot.delay(200);
  robot.keyPress(KeyEvent.VK_L);
  robot.keyRelease(KeyEvent.VK_L);
  robot.delay(200);
  robot.keyPress(KeyEvent.VK_D);
  robot.keyRelease(KeyEvent.VK_D);
  robot.delay(200);
 
  //移动鼠标选择自动穿件main函数
  robot.mouseMove(533, 448);
  robot.mousePress(KeyEvent.BUTTON1_MASK);
  robot.mouseRelease(KeyEvent.BUTTON1_MASK);
  robot.delay(500);
  //回车创建完成
  robot.keyPress(KeyEvent.VK_ENTER);
  robot.keyRelease(KeyEvent.VK_ENTER);
  robot.delay(500);
  //移动光标到输入代码的行上
  robot.keyPress(KeyEvent.VK_DOWN);
  robot.keyRelease(KeyEvent.VK_DOWN);
  robot.delay(500);
  robot.keyPress(KeyEvent.VK_DOWN);
  robot.keyRelease(KeyEvent.VK_DOWN);
  robot.delay(500);
  robot.keyPress(KeyEvent.VK_DOWN);
  robot.keyRelease(KeyEvent.VK_DOWN);
  robot.delay(500);
  robot.keyPress(KeyEvent.VK_DOWN);
  robot.keyRelease(KeyEvent.VK_DOWN);
  robot.delay(500);
  robot.keyPress(KeyEvent.VK_DOWN);
  robot.keyRelease(KeyEvent.VK_DOWN);
  robot.delay(500);
  robot.keyPress(KeyEvent.VK_DOWN);
  robot.keyRelease(KeyEvent.VK_DOWN);
  robot.delay(500);

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/b4faea8bff6ed977f4d02ae0a4068bf3.html