2014-03-04 80 views
1

我有一個非常簡單的問題,或者至少我認爲它很簡單。目前我正在嘗試使用帶變量的機器人類。我的意思是以下(變量「通行證」的字符。)使用變量的Java機器人類

  pass1 = 0; 
      pass2 = 0; 
      pass3 = 0; 
      pass4 = 0; 


       try{ 
         Robot robot = new Robot(); 
         robot.delay(2000); 

         robot.mouseMove(1318, 322); 
         robot.keyPress(pass1); 
         robot.keyPress(pass2); 
         robot.keyPress(pass3); 
         robot.keyPress(pass4); 
         } catch (AWTException e) {e.printStackTrace();} 

當我運行程序時,我得到這個錯誤(記住,我只得到這個錯誤時代碼的一部分用 「PASS1,PASS2 ...等。」 是在它):

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Invalid key code 
at java.awt.Robot.checkKeycodeArgument(Unknown Source) 
at java.awt.Robot.keyPress(Unknown Source) 
at Cracker$2.mouseReleased(Cracker.java:117) 
at java.awt.AWTEventMulticaster.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$000(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

如果PASS1-PASS4是炭灰,不應該'他們是pass1 ='0'等? – Solace

+0

'0'是一個有效的值嗎?因爲那就是你傳給每個keyPress(傳遞#)的東西' – aliteralmind

+0

@solace整數0可以轉換爲char。這仍然不一定使它們成爲機器人方法的有效參數值。 –

回答

1

Robot#keyPress期望一個int,其表示如在java.awt.KeyEvent

具體描述的虛擬鍵碼,鍵0表示KeyEvent.VK_UNDEFINED這是一個「無效的鍵碼」

不要忘了,直到你調用keyRelease,按鍵將繼續壓制,產生重複鍵按下事件......

+0

你有解決這個問題的簡單方法嗎?到目前爲止,我想不出一個。 –

+0

問題是,爲什麼要向'keyPress'方法發送'0',你希望實現什麼? – MadProgrammer

+0

我試圖使KeyPress方法類型爲「0」或「1」 –