I would like to detect keyboard shortcuts like CTRL+S in Scala. It is easy if only one key is pressed, but it seems to be difficult if two or more keys are pressed. Is there any better solution than the following?
reactions += {
case c @ KeyReleased(_, Key.S, _, _) =>
if (c.peer.isControlDown())
// CTRL+S pressed
}
It feels somehow semantically incorrect since it checks if the CTRL button is pressed after the S button is released (and I think it is not really better when you use KeyPressed
or KeyTyped
).
Here is a 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")
}
}
}