Issue
I would like to create an event handler that listens for multiple key combinations such as holding Ctrl and C at the same time.
Why doesn't something like if((... == Control) && (... == C))
work?
Here is the code I trying to work with:
textField.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
public void handle(KeyEvent event) {
if ((event.getCode() == KeyCode.CONTROL) && (event.getCode() == KeyCode.C)) {
System.out.println("Control pressed");
}
};
});
Solution
One way to tackle this problem is to create a KeyCombination
object and set some of its properties to what you see below.
Try the following:
textfield.getScene().getAccelerators().put(new KeyCodeCombination(
KeyCode.C, KeyCombination.CONTROL_ANY), new Runnable() {
@Override public void run() {
//Insert conditions here
textfield.requestFocus();
}
});
Answered By - Anshul Parashar