2012-12-21 187 views
11

使用Java機器人時,發送斜槓或反斜槓時會引發異常。用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."); } 


} 

我在控制檯中看到兩個錯誤信息。順便說一句,我試圖發送的所有其他擊鍵工作正常。

我得到斜線follwowing堆棧跟蹤:

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()'的結果。 – Vulcan

+2

那麼,而不是隻是打印出那裏*是一個例外,爲什麼不打印出例外的細節? –

+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

+1,創造性思維。如果這是您的最終解決方案,請將其標記爲已接受 – Vulcan

+0

爲了改進,不要在每次調用該方法時都使用新的'Robot'。 – Vulcan

+0

@Vulcan這是一個好主意......順便說一下,感謝+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) 
}