Java Robot 类的一些问题


import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
public class sample
{
    public static void main(String args[]) throws AWTException {
        Robot robot = new Robot();
        WindowElement StopTime = handler.findElementByAutomationID(Timer, "2049");
        handler.click(StopTime);
        handler.setfocus(StopTime);
        int StopTimeMinute = Minute + 8 ;
        String val = "";
        val = String.valueOf(StopTimeMinute);
        sendkeys(val);
    }
    public static void sendkeys(String text)
    {
        try {
            Robot robot = new Robot();
            String val = text.toUpperCase();
            for(int i=0;i<val.length();i++) {
                robot.keyPress(Character.getNumericValue(val.charAt(i)));
            }
        } catch(java.awt.AWTException exc) {
            System.out.println("error");
        }
    }
}

在上面的代码中,如果我尝试将变量发送到机器人键事件方法,我会收到找不到源代码的错误。 => 89。Java 机器人无法按键。谁能告诉我如何将变量传递给Robot.KeyPress(code)?

下面的代码有什么问题?Robot.KeyPress(VK_SHIFT) 工作正常,但 Robot.KeyPress(code) 抛出以下错误。

WRobotPeer.keypress int(line) : not available [native method]
Source not found.

我什至尝试发送整数作为参数仍然是相同的问题。

public static void StopMinute(int StopMinute) throws AWTException{
    Robot robot = new Robot();
    robot.delay(20);
    robot.keyPress(StopMinute);
    robot.keyRelease(StopMinute);
}

谁能建议我这个。机器人按键(代码)

字符上的 getNumericValue() 方法返回该字符的 Unicode 代码点。 例如,Character.getNumericValue('A')返回 10,而KeyEvent.VK_A返回 ASCII 值 65。后一个值用于AWT机器人,而不是第一个。

与其遍历val.length(),不如val.toCharArray(),并迭代它。然后将(int)charArray[i]传递给机器人按键。

最新更新