使用Java发送斜杠和反斜杠

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)

我有没有办法发送斜杠和反斜杠按键?


9
请务必在适当的情况下包含e.printStackTrace()的结果。 - FThompson
2
为什么不打印异常的详细信息,而只是打印出有一个异常呢? - Jon Skeet
1
其他按键是否正常工作,还是只有斜杠和反斜杠出现问题? - Quetzalcoatl
1
计算机上的区域设置是什么? - maverik
1
如果您能在问题中附上您的操作系统/JVM版本和构建详细信息,那将非常好,因为它们可能与该问题有关。(这两种方法在我的64位Windows 7,Java 7u7上都可以正常执行,没有异常出现)。 - FThompson
显示剩余14条评论
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);

没有例外。 同时也适用于所有其他字符。但使用时请确保数字锁定功能处于打开状态。


1
+1,创造性思维。如果这是您的最终解决方案,请将其标记为已接受。 - FThompson
为了改进,不要在每次调用该方法时使用新的“Robot”。 - FThompson
这是我的最终解决方案,但网站不让我选择它,它说我必须等待2天;那我就等待然后再选择。 - borges

2

(抱歉,我应该提一下这不是一个完整的答案,只是说对我来说它确实有效,所以我猜测这可能是配置问题或其他什么问题——但我认为其他人可能会发现它很方便。它应该是一个评论,但由于技术原因,我必须将其作为答案)

我之前没有用过Robot类,刚刚花了愉快的半个小时在这个很酷的Java类上进行了一些实验和构建。

对我来说,发送斜杠和反斜杠都可以正常工作。由于VK_???映射到ASCII字符非常好,因此您可以发送'\'或'/',它也应该可以正常工作。

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

这必须从“特权”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 //通常情况下,0就可以工作,但有时太快了。
// 这将使你通过“Alt-Tab”切换到上一个应用程序。测试时我在notepad++中编辑这个, // 然后切换到shell来执行它,这样就能回到我的编辑器并输入“test”文本。 alt VK_TAB
send "反斜杠=\\ \n正斜杠=/"
// 这将发送任何字符串 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) } }
// 这些将适用于整数和字符,而不是字符串 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) }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接