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 を使用している場合に機能します。ただし、編集可能なコンボボックス フィールドで同様の問題が発生します。

編集:

さらに調査すると、テキストフィールドでも発生するようです。テキストフィールドの代わりに次のカスタムクラスを使用して回避しました:

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

MenuBar など、別の場所にアクセラレータをセットアップしている場合、他のソリューションを使用すると、ショートカットが 2 回実行されます。

私の場合、ショートカット (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 に答える