In my javafx program is a popup which lets user press keys and then it sets label accordingly. My problem is with key combinations that are shortcuts for underlying OS for example if user presses Win+R then Run.exe starts but my program should just set the label to "Win+R". My question is how to stop keyevents from triggering OS shortcuts.
Here is the relevant code.
public void showInput() {
Set codes = new HashSet();
Stage inputWindow = new Stage();
GridPane pane = new GridPane();
Scene scene = new Scene(pane);
Label label = new Label("Here comes the pressed keys");
scene.setOnKeyPressed(e -> {
e.consume();
int code = e.getCode().ordinal();
if (label.getText().equals("Here comes the pressed keys")){
codes.add(code);
label.setText(String.valueOf(e.getCode().getName()));
} else if (!codes.contains(code)){
codes.add(code);
label.setText(label.getText() + "+" + e.getCode().getName());
}
});
scene.setOnKeyReleased(e -> {
e.consume();
inputWindow.close();
});
pane.add(label, 0, 0);
inputWindow.setScene(scene);
inputWindow.show();
}
I tried e.consume()
but it did not help.