📜  Java AWT 中的机器人类

📅  最后修改于: 2022-05-13 01:55:04.301000             🧑  作者: Mango

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 TypeMethodDescription
BufferedImagecreateScreenCapture(Rectangle screenRect)Creates an image containing pixels read from the screen.
voiddelay(int ms)Sleeps for the specified time.
intgetAutoDelay()Returns the number of milliseconds this Robot sleeps after generating an event.
ColorgetPixelColor(int x, int y)Returns the color of a pixel at the given screen coordinates.
booleanisAutoWaitForIdle()Returns whether this Robot automatically invokes waitForIdle after generating an event.
voidkeyPress(int keycode)Presses a given key.
voidkeyRelease(int keycode)Releases a given key.
voidmouseMove(int x, int y)Moves mouse pointer to given screen coordinates.
voidmousePress(int buttons)Presses one or more mouse buttons.
voidmouseRelease(int buttons)Releases one or more mouse buttons.
voidmouseWheel(int wheelAmt)Rotates the scroll wheel on wheel-equipped mice.
voidsetAutoDelay(int ms)Sets the number of milliseconds this Robot sleeps after generating an event.
voidsetAutoWaitForIdle(boolean isOn)Sets whether this Robot automatically invokes waitForIdle after generating an event.
voidwaitForIdle()Waits until all events currently on the event queue have been processed. 
 

参考:

  • https://docs.oracle.com/javase/7/docs/api/java Java