4

In my application I have a screen where I use Accelerators. I'm using the function key F3 to execute an operation in my application. It works fine everytime, but when I click in any TextField on this screen the function key doesn't execute.

Here is the code where I set the Accelerator:

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

When I click my textfield and then hit the F3 function key it doesn't work. Someone knows the solution?

4

4 回答 4

4

这适用于我使用 Java 1.8.0_45。但是,我在可编辑的 Combobox 字段中遇到了类似的问题。

编辑:

经过进一步调查,它似乎也出现在文本字段中。我使用以下自定义类代替文本字段来解决它:

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();  
                }
            }
        }); 
    }
}
于 2015-05-01T02:50:04.000 回答
1

这个答案是基于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()]));
}

编辑:修复消费者拼写。

于 2015-07-05T17:15:24.643 回答
0

如果您在其他位置设置了加速器,例如菜单栏,则使用其他一些解决方案将导致快捷方式执行两次。

在我的情况下,在我的快捷键(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();
                    }
                }
            }
        });
    }
}

编辑:Java 9 中没有修复修饰符快捷方式。鉴于 JDK 8 现在很流行,我们可能应该提供一些向后的功能或请求用户将 JRE 更新到 9+。

于 2017-11-27T16:10:48.303 回答
-1

我正在使用 JavaFx+Scene 构建器,然后只需创建一个函数,将所有事件重定向到相应的函数,然后使用 OnKeyReleased 事件将此函数添加到场景中的每个元素

于 2021-07-22T03:54:00.497 回答