使用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);
}
然后,当我想输入那些时,我使用:
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."); }
}
我在控制台中收到两条错误消息。顺便说一句,我试图发送的所有其他击键工作正常。
我得到了斜线的下面的堆栈跟踪:
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 com.paschoalinoto.bruno.pastescript.Paste.slash(Paste.java:23)
at com.paschoalinoto.bruno.pastescript.Paste.type(Paste.java:36)
at com.paschoalinoto.bruno.pastescript.MainGUI$4.actionPerformed(MainGUI.java:113)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
反斜杠工作,但也抛出IllegalArgumentException:
java.lang.IllegalArgumentException: Invalid key code
at sun.awt.windows.WRobotPeer.keyPress(Native Method)
at java.awt.Robot.keyPress(Unknown Source)
at com.paschoalinoto.bruno.pastescript.Paste.press(Paste.java:198)
at com.paschoalinoto.bruno.pastescript.Paste.paste(Paste.java:173)
at com.paschoalinoto.bruno.pastescript.Paste.finalPaste(Paste.java:227)
at com.paschoalinoto.bruno.pastescript.MainGUI$4.actionPerformed(MainGUI.java:113)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
有什么方法可以发送斜杠和反斜杠击键吗?
答案 0 :(得分:4)
好的,我找到了一个解决方案,对于使用不同键盘布局的用户来说可能很方便。它使用Alt代码。
public static void alt(int event1, int event2, int event3, int event4) throws Exception {
Robot bot = new Robot();
bot.delay(50); //Optional
bot.keyPress(KeyEvent.VK_ALT);
bot.keyPress(event1);
bot.keyRelease(event1);
bot.keyPress(event2);
bot.keyRelease(event2);
bot.keyPress(event3);
bot.keyRelease(event3);
bot.keyPress(event4);
bot.keyRelease(event4);
bot.keyRelease(KeyEvent.VK_ALT);
}
然后你这样称呼它:
对于反斜杠:
alt(KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD9, KeyEvent.VK_NUMPAD2);
对于普通人:
alt(KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD0, KeyEvent.VK_NUMPAD4, KeyEvent.VK_NUMPAD7);
没有例外。 也适用于所有其他角色。但请确保在使用时启用Num Lock。
答案 1 :(得分:2)
(对不起,我应该提到这不是一个答案,只是说它确实对我有用,所以我猜它是配置问题或其他什么 - 但我认为这是其他人可能会发现的方便。它应该是一个评论,但由于技术原因,我必须作出答复)
我没有搞乱机器人类,只花了半个小时搞砸了,并在这个很酷的java类之上加入了一些东西。
对我来说,发送斜杠和反斜杠工作正常。自VK _ ???东西很好地映射到ASCII字符,你可以发送'\'或'/',它也可以工作。
我使用了Groovy,因为这是我现在玩的东西,但这里有一个很好的例子和一堆可重复使用的代码。它是作为脚本编写的,但可以很容易地转换为Groovy或Java中的类(我很快就会这样做)。
必须从“Priviliged”shell运行(例如右键单击命令提示符并选择“以管理员身份运行”)。
还必须有时间让你放开键盘! (了解到困难的方法),所以如果你使用groovyShell并使用alt-r来运行它,请确保在发送第一个键之前放置1秒延迟,否则你的ALT将成为按下的键的一部分。
import java.awt.* import java.awt.event.* import static java.awt.event.KeyEvent.* r=new Robot() r.autoWaitForIdle = true r.autoDelay=200 // Usually works with 0 but sometimes that's too fast. // This will alt-tab you to your "Previous" app. While testing I edited this in notepad++ // then tabbed out to a shell to execute it, this tabbed back into my editor and typed // the "test" text. alt VK_TAB send "backslash=\\ \nforward slash =/" // This will send any string def send(String s) { def difference = ("a" as Character) - ("A" as Character) s.each { Character c=it as Character if(c.isUpperCase()) { shift c } else if(c.isLowerCase()) { send(c - difference) } else send(c) } } // These will work for integers and chars, NOT strings def send(key) { press(key as Integer) release(key as Integer) } def alt(key) { press VK_ALT send key release VK_ALT } def shift(key) { press VK_SHIFT send key release VK_SHIFT } def press(key) { r.keyPress(key as Integer) } def release(int key) { r.keyRelease(key as Integer) }