使用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)