小编bru*_*uno的帖子

用Java发送斜杠和反斜杠

使用Java机器人时,发送斜杠或反斜杠会引发异常.

例如:

public void slash() throws AWTException {
    Robot rob = new Robot();
    rob.keyPress(KeyEvent.VK_SLASH);
    rob.keyRelease(KeyEvent.VK_SLASH);
}

public void backSlash() throws AWTException {
    Robot rob = new Robot();
    rob.keyPress(KeyEvent.VK_BACK_SLASH);
    rob.keyRelease(KeyEvent.VK_BACK_SLASH);
}
Run Code Online (Sandbox Code Playgroud)

然后,当我想输入那些时,我使用:

public void type() {

    try {
        slash();
    } catch (AWTException e) { System.out.println("Exception when typing slash."); }

    try {
        backSlash();
    } catch (AWTException e) { System.out.println("Exception when typing back slash."); }


}
Run Code Online (Sandbox Code Playgroud)

我在控制台中收到两条错误消息.顺便说一句,我试图发送的所有其他击键工作正常.

我得到了斜线的foltrawow堆栈跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Invalid key code
    at sun.awt.windows.WRobotPeer.keyPress(Native Method)
    at java.awt.Robot.keyPress(Unknown Source)
    at …
Run Code Online (Sandbox Code Playgroud)

java awtrobot

11
推荐指数
1
解决办法
3430
查看次数

标签 统计

awtrobot ×1

java ×1