2016-12-27 23 views
1

我想弄清楚如何讓Scala Swing對同時發生的多個關鍵事件做出反應。我知道Swing如何檢測按下的一個按鍵,但是例如如何檢測兩個按鍵是否同時按下?注:未安裝Java經驗Scala/Swing - 響應同時發生的多個關鍵事件

我知道的第一個事件是不行的,但我儘量代表什麼,我想用它來完成:

reactions += { 
      //case KeyPressed(_, Key.Space && Key.Up, _, _) 
       //label.text = "Space and Up are down" 
      case KeyPressed(_, Key.Space, _, _) => 
       label.text = "Space is down" 
      case KeyPressed(_, Key.Up, _, _) => 
       label.text = "Up is down" 

     } 

任何想法可能幫助?或者直接回答如何去做?

+1

可能出現[Swing的KeyListener和sa按下的多個按鍵我的時間](http://stackoverflow.com/questions/2623995/swings-keylistener-and-multiple-keys-pressed-at-the-same-time) –

回答

0

請保存所有被壓

var pressedKeys = Buffer[Key.Value]() 

當按鍵被按下添加的關鍵緩衝,並檢查緩衝區包含一些想鍵值

reactions += { 
      case KeyPressed(_, key, _, _) => 
       pressedKeys += key 
       if(pressedKeys contains Key.Space){ //Do if Space 
        label.text = "Space is down" 
        if(pressedKeys contains Key.Up) //Do if Space and Up 
        label.text = "Space and Up are down" 
       }else if(pressedKeys contains Key.Up)//Do if Up 
        label.text = "Up is down" 

清除按鍵緩衝區釋放按鈕時的緩衝區

  case KeyReleased(_, key, _, _) => 
        pressedKeys = Buffer[Key.Value]() 
        /* I tried only to remove the last key with 
        * pressedKeys -= key, but ended up working 
        *badly, because in many occasions the program 
        *did not remove the key*/