I want my application (JavaFx 14) to so something when the key "a" is pressed and I also want the application to do something when a key combination with the same key "a" and "b" is pressed. The goal is to only call one method. the method for pressing a or the method for pressing a +b. I tried doing it with a observable List with keycodes but all methods are called.
private ObservableList<KeyCode> keys = FXCollections.observableArrayList();
private void keyboardShortcuts(Button[] buttons){
// collect pressed key
scene.addEventFilter(KeyEvent.KEY_PRESSED, (e) -> {
if (!keys.contains(e.getCode())) {
keys.add(e.getCode());
}
e.consume();
});
// remove pressed key when it is released
scene.setOnKeyReleased((e) -> {
keys.remove(e.getCode());
e.consume();
});
keys.addListener(new InvalidationListener() {
@Override
public void invalidated(Observable observable) {
if(keys.size() == 1) {
if(keys.contains(KeyCode.A)) {
foo();
}
if(keys.contains(KeyCode.B)) {
foo2();
}
}
if(keys.size() == 2) {
if(keys.contains(KeyCode.A) && keys.contains(KeyCode.B)) {
foo3();
}
}
}
});
}