JAVA Robot 机器人工具类
什么是机器人工具类
English
-
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.
chinese
- 此类用于生成本地系统输入事件,以用于测试自动化、自运行演示以及需要控制鼠标和键盘的其他应用程序。 Robot 的主要目的是促进 Java 平台实现的自动化测试。
- 使用该类生成输入事件与将事件发布到 AWT 事件队列或 AWT 组件的不同之处在于,事件是在平台的本机输入队列中生成的。例如,Robot.mouseMove 将实际移动鼠标光标,而不仅仅是生成鼠标移动事件。
- 请注意,某些平台需要特殊权限或扩展才能访问低级输入控制。如果当前平台配置不允许输入控制,则在尝试构造 Robot 对象时将抛出 AWTException。例如,如果 X 服务器不支持(或未启用)XTEST 2.2 标准扩展,则 X-Window 系统将抛出异常。
注
此工具类与qq机器人并不相同,可以理解为键盘与鼠标事件,代替人对电脑执行操作
具体使用
构造方法
无参
/**
* Constructs a Robot object in the coordinate system of the primary screen.
*在主屏幕的坐标系中构造一个 Robot 对象。
* @throws AWTException if the platform configuration does not allow
* low-level input control. This exception is always thrown when
* GraphicsEnvironment.isHeadless() returns true
* @throws SecurityException if {@code createRobot} permission is not granted
* @see java.awt.GraphicsEnvironment#isHeadless
* @see SecurityManager#checkPermission
* @see AWTPermission
*/
public Robot() throws AWTException {
checkHeadless();
init(GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice());
}
有参
/**
* Creates a Robot for the given screen device. Coordinates passed
* to Robot method calls like mouseMove, getPixelColor and
* createScreenCapture will be interpreted as being in the same coordinate
* system as the specified screen. Note that depending on the platform
* configuration, multiple screens may either:
* <ul>
* <li>share the same coordinate system to form a combined virtual screen</li>
* <li>use different coordinate systems to act as independent screens</li>
* </ul>
* <p>
* If screen devices are reconfigured such that the coordinate system is
* affected, the behavior of existing Robot objects is undefined.
*
为给定的屏幕设备创建一个机器人。传递给 Robot 方法调用(如 mouseMove、getPixelColor 和 createScreenCapture)的坐标将被解释为与指定屏幕在同一坐标系中。请注意,根据平台配置,多个屏幕可能: 共享相同的坐标系以形成组合的虚拟屏幕 使用不同的坐标系作为独立的屏幕 如果重新配置屏幕设备以致坐标系受到影响,现有的行为机器人对象未定义。
* @param screen A screen GraphicsDevice indicating the coordinate
* system the Robot will operate in.
* @throws AWTException if the platform configuration does not allow
* low-level input control. This exception is always thrown when
* GraphicsEnvironment.isHeadless() returns true.
* @throws IllegalArgumentException if {@code screen} is not a screen
* GraphicsDevice.
* @throws SecurityException if {@code createRobot} permission is not granted
* @see java.awt.GraphicsEnvironment#isHeadless
* @see GraphicsDevice
* @see SecurityManager#checkPermission
* @see AWTPermission
*/
public Robot(GraphicsDevice screen) throws AWTException {
checkHeadless();
checkIsScreenDevice(screen);
init(screen);
}
非构造传入屏幕对象
private void init(GraphicsDevice screen) throws AWTException {
checkRobotAllowed();
Toolkit toolkit = Toolkit.getDefaultToolkit();
if (toolkit instanceof ComponentFactory) {
peer = ((ComponentFactory)toolkit).createRobot(screen);
}
initLegalButtonMask();
}
事件
键盘点击事件
点击
方法
public synchronized void keyPress(int keycode) {
checkKeycodeArgument(keycode);
peer.keyPress(keycode);
afterEvent();
}
Presses a given key. The key should be released using the keyRelease method.
按下传入的按键,这个建应该被释放方法释放
释放
public synchronized void keyRelease(int keycode) {
checkKeycodeArgument(keycode);
peer.keyRelease(keycode);
afterEvent();
}
Releases a given key.
封装方法
单击:
public void clickKey(int Key,int sleep) throws InterruptedException {
robot.keyPress(Key);
Thread.sleep(sleep);
robot.keyRelease(Key);
}
组合键:
public void clickMore(int[] keys,int sleep) throws InterruptedException {
for (int i : keys) {
robot.keyPress(i);
System.out.println(i);
}
Thread.sleep(sleep);
for (int i : keys) {
robot.keyRelease(i);
System.out.println(i);
}
}
测试—qq轰炸机:
鼠标事件
鼠标移动
public synchronized void mouseMove(int x, int y) {
peer.mouseMove(x, y);
afterEvent();
}
Moves mouse pointer to given screen coordinates(坐标).
点击
public synchronized void mousePress(int buttons) {
checkButtonsArgument(buttons);
peer.mousePress(buttons);
afterEvent();
}
释放
public synchronized void mouseRelease(int buttons) {
checkButtonsArgument(buttons);
peer.mouseRelease(buttons);
afterEvent();
}
封装
public void clickMouse(int x,int y,int Key,int count,int sleep){
robot.mouseMove(x,y);
for(int i = 0;i<count;i++){
robot.mousePress(Key);
robot.mouseRelease(Key);
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
截屏
public boolean screenshot(int x,int y,int _X,int _Y,String file) throws FileNotFoundException {
BufferedImage screenCapture = robot.createScreenCapture(new Rectangle(x, y, _X, _Y));
File file1 = new File(file);
OutputStream os = new FileOutputStream(file1);
try {
ImageIO.write(screenCapture,"png",os);
} catch (IOException e) {
return false;
}
return true;
}
RobotUtil类(没什么玩的了,脑残才拿这个写挂)
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
public class RobotUtil {
Robot robot;
public RobotUtil(Robot robot){
this.robot = robot;
}
public void clickKey(int Key,int sleep) throws InterruptedException {
robot.keyPress(Key);
Thread.sleep(sleep);
robot.keyRelease(Key);
}
public void clickMore(int[] keys,int sleep) throws InterruptedException {
for (int i : keys) {
robot.keyPress(i);
}
Thread.sleep(sleep);
for (int i : keys) {
robot.keyRelease(i);
}
}
public void clickMouse(int x,int y,int Key,int count,int sleep){
robot.mouseMove(x,y);
for(int i = 0;i<count;i++){
robot.mousePress(Key);
robot.mouseRelease(Key);
try {
Thread.sleep(sleep);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public boolean screenshot(int x,int y,int _X,int _Y,String file) throws FileNotFoundException {
BufferedImage screenCapture = robot.createScreenCapture(new Rectangle(x, y, _X, _Y));
File file1 = new File(file);
OutputStream os = new FileOutputStream(file1);
try {
ImageIO.write(screenCapture,"png",os);
} catch (IOException e) {
return false;
}
return true;
}
}