JavaFX InputMap/ActionMap equivalent?

前端 未结 2 423
一生所求
一生所求 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
    0 讨论(0)
  • 2021-01-16 15:03

    Aha.

    This question, from a year ago, already relating to Java 9, reveals what looks like future existence in JavaFX of com.sun.javafx.scene.control.inputmap.InputMap... obviously at the moment package com.sun.javafx.scene.control.inputmap does not exist (in Java 8) (unless I'm very much mistaken).

    The person who posed that question, Kleopatra, is something of an expert in Java Swing, and presumably now in JavaFX. We see some early releases of Java 9 happening around now, March 2017, precisely... but having tried googling for the API Javadoc for JavaFX 9, I can't as yet see any sign of com.sun.javafx.scene.control.inputmap.InputMap.

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