我想在 Scala 中检测像 CTRL+S 这样的键盘快捷键。如果只按下一个键很容易,但如果按下两个或多个键似乎很难。有没有比以下更好的解决方案?
reactions += {
case c @ KeyReleased(_, Key.S, _, _) =>
if (c.peer.isControlDown())
// CTRL+S pressed
}
KeyPressed
感觉在语义上有点不正确,因为它检查是否在释放 S 按钮后按下了 CTRL 按钮(我认为使用or并没有更好KeyTyped
)。
这是一个SSCE:
import scala.swing._
import scala.swing.event._
object SSCCE extends SimpleSwingApplication {
def top = new MainFrame {
val textArea = new TextArea(3, 30)
contents = new FlowPanel {
contents += textArea
}
listenTo(textArea.keys)
reactions += {
case c @ KeyReleased(_, Key.S, _, _) =>
if (c.peer.isControlDown())
Dialog.showMessage(null, "Message", "CTRL+S pressed")
}
}
}