11

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

有什么方法可以发送斜杠和反斜杠按键吗?

4

2 回答 2

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。

于 2012-12-21T22:05:37.800 回答
2

(对不起,我应该提到这不是一个完整的答案,只是说它确实对我有用,所以我猜这是配置问题或其他问题 - 但我认为这是其他人可能会觉得方便的东西。它应该是评论,但出于技术原因,我必须将其作为答案)

我没有搞乱 Robot 类,只是花了半个小时在这个很酷的 java 类上乱搞和添加一些东西。

对我来说发送斜杠和反斜杠工作正常。自从VK_???东西很好地映射到 ASCII 字符,你可以发送 '\' 或 '/' 它也应该可以工作。

我使用 Groovy 是因为这就是我最近玩的东西,但这里有一个很好的例子和一堆我刚刚想出的可重用代码。它是作为脚本编写的,但可以很容易地转换为 Groovy 或 Java 中的类(我很快就会这样做)。

这必须从“特权”外壳运行(例如,右键单击命令提示符并选择“以管理员身份运行”)。

它也必须有时间让你放开键盘!(学到了很难的方法),所以如果你使用 groovyShell 并使用 alt-r 运行它,请确保在发送第一个键之前延迟 1 秒,否则你的 ALT 将成为按下的键的一部分。

导入 java.awt.*
导入 java.awt.event.*
导入静态 java.awt.event.KeyEvent.*

r=新机器人()
r.autoWaitForIdle = true
r.autoDelay=200 // 通常使用 0 但有时太快了。

// 这将 alt-tab 你到你的“上一个”应用程序。在测试时我在记事本++中编辑了这个
// 然后切换到 shell 来执行它,这个切换回我的编辑器并输入
// “测试”文本。
alt VK_TAB

发送“反斜杠=\\\n正斜杠=/”

// 这将发送任何字符串
def 发送(字符串 s)
{
    def 差异 = ("a" as Character) - ("A" as Character)
    s.每个{
        字符 c=它作为字符
        如果(c.isUpperCase()){
            班次 c
        }否则如果(c.isLowerCase()){
            发送(c - 差异)
        }
        否则发送(c)
    }
}

// 这些将适用于整数和字符,而不是字符串
定义发送(键)
{
    按(键为整数)
    释放(键为整数)
}
默认替代(键)
{
    按 VK_ALT
    发送密钥
    释放 VK_ALT
}
def shift(键)
{
    按 VK_SHIFT
    发送密钥
    释放 VK_SHIFT
}

默认按下(键)
{
    r.keyPress(键为整数)
}
def 释放(int 键)
{
    r.keyRelease(键为整数)
}
于 2012-12-21T18:32:28.527 回答