Scala/Swing - 响应同时发生的多个关键事件
Scala/Swing - Responding to multiple key events that are happening at the same time
我想弄清楚如何让 Scala Swing 对同时发生的多个关键事件做出反应。我知道 Swing 如何检测按下的一个键,但是例如它如何检测是否同时按下两个键?注:无Java经验
我知道第一个事件不起作用,但我尝试用它来表示我正在尝试完成的事情:
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"
}
有什么可能有用的想法吗?或者直接回答怎么做?
创建一个缓冲区来保存所有按下的键
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*/
我想弄清楚如何让 Scala Swing 对同时发生的多个关键事件做出反应。我知道 Swing 如何检测按下的一个键,但是例如它如何检测是否同时按下两个键?注:无Java经验
我知道第一个事件不起作用,但我尝试用它来表示我正在尝试完成的事情:
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"
}
有什么可能有用的想法吗?或者直接回答怎么做?
创建一个缓冲区来保存所有按下的键
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*/