JavaFX - accelerator does not work when text field has focus

In my application, I have a screen where I use Accelerators. I use the F3 function key to perform an operation in my application. It works great every time, but when I click on any text field on this screen, the function key is not executed.

Here is the code where I install Accelerator:

    scene.getAccelerators().put(
            new KeyCodeCombination(KeyCode.F3),
            new Runnable() {
                @Override
                public void run() {
                    // do sth here
                }
            }
    );

When I press my text box and then press the F3 function key, it does not work. Does anyone know a solution?

+4
source share
3 answers

This works for me using Java 1.8.0_45. However, I am facing a similar problem with the Combobox editable field.

:

. , :

public class ShortcutFriendlyTextField extends TextField{

    public ShortcutFriendlyTextField() {
        super();
        addEventHandler(KeyEvent.KEY_RELEASED,new EventHandler<KeyEvent>() {

            @Override
            public void handle(KeyEvent event) {
                //handle shortcuts if defined
                Runnable r=getScene().getAccelerators().get(new KeyCodeCombination(event.getCode()));
                if(r!=null) {
                    r.run();  
                }
            }
        }); 
    }
}
+4

tikerman. -.

if (!event.getCode().isModifierKey()) {
    Consumer<KeyCombination.Modifier[]> runAccelerator = (modifiers) -> {
        Runnable r = getScene().getAccelerators().get(new KeyCodeCombination(event.getCode(), modifiers));
        if (r != null) {
            r.run();
        }
    };

    List<KeyCombination.Modifier> modifiers = new ArrayList<>();
    if (event.isControlDown()) modifiers.add(KeyCodeCombination.CONTROL_DOWN);
    if (event.isShiftDown()) modifiers.add(KeyCodeCombination.SHIFT_DOWN);
    if (event.isAltDown()) modifiers.add(KeyCodeCombination.ALT_DOWN);

    runAccelerator.apply(modifiers.toArray(new KeyCombination.Modifier[modifiers.size()]));
}

: .

+1

, , , MenuBar, , , .

(SHIFT + F3) , , F3.

, .

public  static final double JAVA_VERSION = Double.parseDouble(System.getProperty("java.specification.version"));
public static void makeInputFieldShortCutFriendly(Node node) {
    //this bug wasn't fixed until 9.0
    if (JAVA_VERSION < 9) {
        node.addEventHandler(KeyEvent.KEY_RELEASED, (KeyEvent event) -> {
            if (!event.getCode().isModifierKey()) {
                if (!event.isAltDown() && !event.isShiftDown() && !event.isAltDown()) {
                    Runnable r = node.getScene().getAccelerators().get(new KeyCodeCombination(event.getCode(), KeyCodeCombination.SHORTCUT_ANY));

                    if (r != null) {
                        r.run();
                    }
                }
            }
        });
    }
}

Edit: In Java, modifier shortcuts were not fixed. Given the popularity of JDK 8 right now, we should probably provide some backward opportunity or ask users to upgrade the JRE there to 9+.

0
source

All Articles