I have a JavaFX application that uses drag & drop (as described in this tutorial). This application also uses a KeyEvent-filter to change its state when a certain key is pressed or released.
For example, I use this example from the official Drag & Drop tutorial and add the following handlers during the start method:
public class HelloDragAndDrop extends Application {
// ...
@Override public void start(Stage stage) {
// ...
stage.addEventFilter(KeyEvent.KEY_PRESSED, event -> {
if (event.getCode() == KeyCode.R)
scene.setFill(Color.RED);
});
stage.addEventFilter(KeyEvent.KEY_RELEASED, event -> {
if (event.getCode() == KeyCode.R)
scene.setFill(Color.LIGHTGREEN);
});
// ...
}
// ...
}
In general, this will make the background turn red while I hold down the R key and green again when I release it. But here's the problem: no KeyEvents are dispatched during the drag & drop action. Suppose the following user actions:
- user presses R -> background turns red
- user starts dragging
- user releases R -> background remains red
- user finishes dragging -> background remains red
The only way to get back to the regular color is to press and release R again.
Main question: is there any way to receive those KeyEvents during the drag&drop action?
WorkAroundQuestion: in case I have to work with the fact that those KeyEvents might be "lost" - is there a way I can do something like boolean isKeyDown(KeyCode keyCode)
so i can at least adjust the application's state after the drag&drop action is finished?
(P.S.: this is not the same question as that question as this is about drag&drop while that question is about general drag gestures.)