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;
}
}