用Java发送斜杠和反斜杠
使用Java robot时,发送斜杠或反斜杠时会引发异常 例如:用Java发送斜杠和反斜杠,java,awtrobot,Java,Awtrobot,使用Java robot时,发送斜杠或反斜杠时会引发异常 例如: 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 R
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)
有什么方法可以发送斜杠和反斜杠按键吗?(很抱歉,我应该提到这不是一个很好的答案,只是说它确实对我有用,所以我猜这是一个配置问题或其他问题——但我认为其他人可能会觉得这很方便。这应该是一个注释,但出于技术原因,我必须将其作为一个答案)
我没有搞乱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 // 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)
}
导入java.awt*
导入java.awt.event*
导入静态java.awt.event.KeyEvent*
r=新机器人()
r、 autoWaitForIdle=true
r、 autoDelay=200//通常与0一起工作,但有时速度太快。
//这会将你切换到你的“上一个”应用程序。测试时,我在记事本中编辑了这个++
//然后切换到一个shell来执行它,这个切换回到我的编辑器中并键入
//“测试”文本。
alt VK_选项卡
发送“反斜杠=\\\n前斜杠=/”
//这将发送任何字符串
def发送(字符串s)
{
定义差异=(“a”作为字符)-(“a”作为字符)
s、 各{
字符c=它作为字符
if(c.isUpperCase()){
移位c
}else if(c.isLowerCase()){
发送(c-差异)
}
其他发送(c)
}
}
//这些将适用于整数和字符,而不是字符串
def发送(键)
{
按(键为整数)
释放(键为整数)
}
def alt(键)
{
按VK_ALT
发送密钥
释放VK_ALT
}
def换档(钥匙)
{
按下VK_SHIFT键
发送密钥
释放VK_移位
}
def按下(键)
{
r、 按键(键为整数)
}
def释放(int键)
{
r、 键释放(键为整数)
}
好的,我找到了一个解决方案,它使用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。请始终包含
e.printStackTrace()的结果
适用时。与其打印出异常,不如打印出异常详细信息?其他击键是否有效,或者只是斜杠和反斜杠正在中断?计算机上有什么语言环境?如果您可以将OS/JVM构建和版本详细信息添加到问题中,那也太好了,因为这些可能与问题有关。(这两种方法在64位Windows 7、Java 7u7上执行时没有例外).1,创造性思维。如果这是您的最终解决方案,请将其标记为已接受。不过,为了改进,不要在每次调用该方法时都使用新的机器人。这是我的最终解决方案,但网站不允许我选择,它说我必须等待2天;我会等待,然后选择。