JavaFX InputMap/ActionMap equivalent?

前端 未结 2 432
一生所求
一生所求 2021-01-16 14:02

I\'m finally switching over fully to JavaFX.

I\'m very keen on keystroke functionality.

Is there an equivalent hotkey architecture to the (very good) one yo

2条回答
  •  逝去的感伤
    2021-01-16 14:43

    In JavaFX you can register a callback for KeyPressed events.

    For example:

    myTextField.setOnKeyPressed(event->{
        if (event.getCode() == KeyCode.ENTER){
            //do something here
        }
    }
    

    If you wanted to register a global Key combination (say, the typical Ctrl-S for saving) you could instead use:

    myScene.getAccellerators().put(
        new KeyCodeCombination(KeyCode.S, KeyCombination.CONTROL_DOWN),
                () -> { /** save my work **/ }
        )
    

    List of available KeyCodeCombinations: https://docs.oracle.com/javafx/2/api/javafx/scene/input/KeyCodeCombination.html

    EDIT(1): how event propagation works

    From http://docs.oracle.com/javafx/2/events/processing.htm

    The event delivery process contains the following steps:

    • Target selection
    • Route construction
    • Event capturing
    • Event bubbling

提交回复
热议问题