JavaFX Canvas not picking up key events

前端 未结 2 1534
时光说笑
时光说笑 2021-01-18 02:56

I have the following code showing a JavaFX Canvas with 3 consecutive hello worlds

    StackPane root = new StackPane();

        Canvas canvas = new Canvas(2         


        
2条回答
  •  萌比男神i
    2021-01-18 03:42

    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.

提交回复
热议问题