I have the following code showing a JavaFX Canvas with 3 consecutive hello worlds
StackPane root = new StackPane(); Canvas canvas = new Canvas(2
You need
canvas.setFocusTraversable(true);
as canvases do not have focusTraversable set by default.
Add the following line:
canvas.addEventFilter(MouseEvent.ANY, (e) -> canvas.requestFocus());
After you click your canvas, the canvas will request the focus and recognice key events.