Java AWT 中的机器人类
Java AWT 包中的 Robot 类用于生成本地系统输入事件,用于测试自动化、自运行演示和其他需要控制鼠标和键盘的应用程序。 Robot 的主要目的是促进Java平台实现的自动化测试。简单来说,该类提供对鼠标和键盘设备的控制。
下面是 Robot 类如何控制键盘并在空白记事本文档中键入内容的示例。如本文所述,记事本是使用 Process 和 Runtime 调用的。
Java
// Java program to demonstrate working of Robot
// class. This program is for Windows. It opens
// notepad and types a message.
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.*;
public class robo
{
public static void main(String[] args) throws IOException,
AWTException, InterruptedException
{
String command = "notepad.exe";
Runtime run = Runtime.getRuntime();
run.exec(command);
try {
Thread.sleep(2000);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
// Create an instance of Robot class
Robot robot = new Robot();
// Press keys using robot. A gap of
// of 500 milli seconds is added after
// every key press
robot.keyPress(KeyEvent.VK_H);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_E);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_L);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_L);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_O);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_SPACE);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_F);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_R);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_O);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_M);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_SPACE);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_G);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_E);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_E);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_K);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_S);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_F);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_O);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_R);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_G);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_E);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_E);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_K);
Thread.sleep(500);
robot.keyPress(KeyEvent.VK_S);
}
}
输出:
The code opens a blank Notepad file and types
"hello from geeksforgeeks" onto it with a delay
of 500 ms before typing out each character.
Robot类的方法:Return Type Method Description BufferedImage createScreenCapture(Rectangle screenRect) Creates an image containing pixels read from the screen. void delay(int ms) Sleeps for the specified time. int getAutoDelay() Returns the number of milliseconds this Robot sleeps after generating an event. Color getPixelColor(int x, int y) Returns the color of a pixel at the given screen coordinates. boolean isAutoWaitForIdle() Returns whether this Robot automatically invokes waitForIdle after generating an event. void keyPress(int keycode) Presses a given key. void keyRelease(int keycode) Releases a given key. void mouseMove(int x, int y) Moves mouse pointer to given screen coordinates. void mousePress(int buttons) Presses one or more mouse buttons. void mouseRelease(int buttons) Releases one or more mouse buttons. void mouseWheel(int wheelAmt) Rotates the scroll wheel on wheel-equipped mice. void setAutoDelay(int ms) Sets the number of milliseconds this Robot sleeps after generating an event. void setAutoWaitForIdle(boolean isOn) Sets whether this Robot automatically invokes waitForIdle after generating an event. void waitForIdle() Waits until all events currently on the event queue have been processed.
参考:
- https://docs.oracle.com/javase/7/docs/api/java Java