2009-10-29 77 views
2

我在我的Java應用程序綁定事件的鍵,但是,我遇到了一個非常奇怪的錯誤,我希望你們可以幫我解決。擊鍵沒有捕捉到左/右,但能夠趕上向上/向下和所有其他鍵

 KeyStroke up = KeyStroke.getKeyStroke("W"); 
     KeyStroke down = KeyStroke.getKeyStroke("S"); 
     KeyStroke right = KeyStroke.getKeyStroke("D"); 
     KeyStroke left = KeyStroke.getKeyStroke("A"); 
     KeyStroke p2_up = KeyStroke.getKeyStroke("UP"); 
     KeyStroke p2_down = KeyStroke.getKeyStroke("DOWN"); 
     KeyStroke p2_left = KeyStroke.getKeyStroke("LEFT"); 
     KeyStroke p2_right = KeyStroke.getKeyStroke("RIGHT"); 
     /*KeyStroke key_1 = KeyStroke.getKeyStroke("1"); 
     KeyStroke key_2 = KeyStroke.getKeyStroke("2"); 
     KeyStroke key_3 = KeyStroke.getKeyStroke("3"); 
     KeyStroke key_4 = KeyStroke.getKeyStroke("4"); 
     KeyStroke key_5 = KeyStroke.getKeyStroke("5"); 
     KeyStroke key_6 = KeyStroke.getKeyStroke("6"); 
     KeyStroke key_7 = KeyStroke.getKeyStroke("7"); 
     KeyStroke key_8 = KeyStroke.getKeyStroke("8"); 
     KeyStroke key_9 = KeyStroke.getKeyStroke("9"); 
     KeyStroke key2_1 = KeyStroke.getKeyStroke("F1"); 
     KeyStroke key2_2 = KeyStroke.getKeyStroke("F2"); 
     KeyStroke key2_3 = KeyStroke.getKeyStroke("F3"); 
     KeyStroke key2_4 = KeyStroke.getKeyStroke("F4"); 
     KeyStroke key2_5 = KeyStroke.getKeyStroke("F5"); 
     KeyStroke key2_6 = KeyStroke.getKeyStroke("F6"); 
     KeyStroke key2_7 = KeyStroke.getKeyStroke("F7"); 
     KeyStroke key2_8 = KeyStroke.getKeyStroke("F8"); 
     KeyStroke key2_9 = KeyStroke.getKeyStroke("F9");*/ 

     InputMap inputMap = content 
       .getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); 

     inputMap.put(up, "up"); 
     inputMap.put(down, "down"); 
     inputMap.put(right, "right"); 
     inputMap.put(left, "left"); 

     inputMap.put(p2_up, "up2"); 
     inputMap.put(p2_down, "down2"); 
     inputMap.put(p2_right, "right2"); 
     inputMap.put(p2_left, "left2"); 

     Bindings KbBinds = new Bindings(this.currentBigBox); 

     content.getActionMap().put("up", KbBinds.moveUp); 
     content.getActionMap().put("down", KbBinds.moveDown); 
     content.getActionMap().put("right", KbBinds.moveRight); 
     content.getActionMap().put("left", KbBinds.moveLeft); 

     content.getActionMap().put("up2", KbBinds.p2MoveUp); 
     content.getActionMap().put("down2", KbBinds.p2MoveDown); 
     content.getActionMap().put("right2", KbBinds.p2MoveRight); 
     content.getActionMap().put("left2", KbBinds.p2MoveLeft); 

基本上除了LEFT/RIGHT以外的所有按鍵都被捕捉並綁定。 ;如果我和 的KeyStroke p2_left = KeyStroke.getKeyStroke( 「UP」)代替本

 KeyStroke p2_left = KeyStroke.getKeyStroke("LEFT"); 

註釋掉p2_up時,p2_left按預期工作。

任何線索?

+1

哇,一個downvote,也許你可以評論爲什麼? – nubela 2009-10-29 17:40:16

+0

+1 Prob下投票鍵/記錄的B/C。鍵入日誌有合理的理由。 – 2009-10-29 17:48:21

+0

VK_LEFT鍵擊會被某些其他組件佔用並丟棄嗎?是否有其他聽衆或組件? – Mikeb 2009-10-29 18:20:26

回答

0

我沒有投票,但我會。原因是您發佈的代碼中有90%在提供有關您問題的信息方面毫無用處。我們不知道你如何使用代碼的背景,或者你正在將綁定添加到什麼組件......

我在做鍵綁定時使用「上,下,左,右」沒有問題。

如果您需要更多幫助,請發送您的SSCCE

0

最有可能的解釋是某些東西在捕捉這些關鍵筆畫。最可能的候選者是某種類型的滾動窗格或輸入文本框。

相關問題