JavaFX InputMap/ActionMap equivalent?

丶灬走出姿态 提交于 2019-12-14 00:41:00

问题


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 you find in Swing? How does it work in the case of a JavaFX text control object? I thought all might become clear if I took a look at javafx.scene.control.TextInputControl. Also tried googling of course.

But I'm none the wiser. Presumably there must be a source of the minimal keystroke bindings you need when editing text? Is it similarly hierarchical?

With Swing every JComponent can use 1 ActionMap and 3 InputMaps (and all of these can have "ancestor" maps). The 3 InputMap "types" are determined by the JComponent constants WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW and WHEN_ANCESTOR_OF_FOCUSED_COMPONENT which are pretty self-explanatory names. I seriously wonder how many Swing users are familiar with all this stuff but personally, being a keyboard fanatic, I use this functionality extensively.

Being able to attach a key binding to a given javafx.scene.Node using setOnKeyPressed is presumably equivalent to using the WHEN_FOCUSED Swing functionality... but what about the other two categories, I wonder? Are they simply absent in the current implementation of JavaFX?

Ultimately, with Swing, behind the scenes there is obviously a very sophisticated mechanism involving keystrokes being "passed up" the hierarchical structure of JComponent objects until they are "caught" by an InputMap (or not)... presumably there must be something a bit like this in JavaFX. It'd be nice to have chapter and verse...


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/42713630/javafx-inputmap-actionmap-equivalent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!