JavaFX Canvas not picking up key events

前端 未结 2 1511
时光说笑
时光说笑 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条回答
  • 2021-01-18 03:21

    You need

    canvas.setFocusTraversable(true);
    

    as canvases do not have focusTraversable set by default.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题