如何禁用JavaFX MenuBar的助记符?

时间:2016-03-15 11:26:18

标签: javafx menubar keyevent mnemonics alt-key

在我的舞台上,我像往常一样在顶部插入一个菜单栏。我想在舞台内的另一个上下文中给出ALT键(连同箭头键)一些逻辑。但每当我按下ALT和箭头时,我也会无意中浏览菜单栏的菜单。

我想避免或更好地完全禁用此助记符行为。 将所有菜单的mnemonicParsing属性设置为false失败。我也试过这种方法但没有成功:

menubar.addEventFilter(KeyEvent.ANY, e -> e.consume());

2 个答案:

答案 0 :(得分:3)

当按下 ALT 时,第一个菜单会聚焦,当菜单有焦点时,箭头键会导致它们之间的导航,无论是否按下 ALT 。因此,为了防止出现这种情况,您需要在按下 ALT 时阻止第一个菜单获得焦点。

查看MenuBarSkin班级'构造函数源代码,给我们解决方案:

public MenuBarSkin(final MenuBar control) {
    ...
    Utils.executeOnceWhenPropertyIsNonNull(control.sceneProperty(), (Scene scene) -> {
        scene.getAccelerators().put(acceleratorKeyCombo, firstMenuRunnable);

        // put focus on the first menu when the alt key is pressed
        scene.addEventHandler(KeyEvent.KEY_PRESSED, e -> {
            if (e.isAltDown()  && !e.isConsumed()) {
                firstMenuRunnable.run();
            }
        });
    });
    ...
}

解决方案:

正如您已经猜到的那样,解决方案是在 ALT 关闭时使用该事件,但您需要将EventHandler添加到scene而不是menubar

scene.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
    @Override
    public void handle(KeyEvent event) {
        // your desired behavior
        if(event.isAltDown())
            event.consume();
    }
});

答案 1 :(得分:0)

或者你可以重写MenuBar皮肤。 当ALT键按下,而不是已发布时,Javafx做出了选择(或是一个错误?),重点是MenuBar,是Eclipse,Netbeans中的标准行为.... 此外,当按下或释放ALT_GRAPH键时,不应该关注MenuBar。

这是我建议的补丁。 请注意,只有第一个差异是相关的,最后一个差异只是当一个人无法访问代码时编译的代码。 基本上我已经拆分了&#34; firstMenuRunnable&#34;在3个函数中

  • 仅在按下F10键时使用firstMenuRunnable

  • 当menuBar具有焦点且ALT键为时,使用deselectOnKeyPressed

  • 当menuBar没有时,使用focusOnFirstMenuOnKeyReleased 焦点和ALT键已发布

因此,人们可以拥有标准行为,允许加速器使用ALT键,而不会被MenuBar 占用。

    --- com/sun/javafx/scene/control/skin/MenuBarSkin.java in C:\Program Files (x86)\Java\jdk1.8.0_131\javafx-src.zip
    +++ C:\Users\daniel\dev\xxx\Layout\src\com\stimulus\control\MenuBarSkin.java     
@@ -372,12 +491,21 @@
             scene.getAccelerators().put(acceleratorKeyCombo, firstMenuRunnable);

             // put focus on the first menu when the alt key is pressed
+            scene.addEventFilter(KeyEvent.KEY_PRESSED, e -> {
+                altDown = false;
+            });
             scene.addEventHandler(KeyEvent.KEY_PRESSED, e -> {
-                if (e.isAltDown()  && !e.isConsumed()) {
-                    firstMenuRunnable.run();
+                if (e.isAltDown() && !e.isConsumed() && e.getCode().equals(KeyCode.ALT)) {
+                    deselectMenusOnKeyPressed.run();
+                    altDown = true;
                 }
             });
+            scene.addEventHandler(KeyEvent.KEY_RELEASED, e -> {
+                if (altDown) {
+                    focusOnFirstMenuOnKeyReleased.run();
+                }
         });
+        });

         ParentTraversalEngine engine = new ParentTraversalEngine(getSkinnable());
         engine.addTraverseListener(this);
    @@ -434,7 +453,50 @@
                 }
             };

    +    private boolean menuDeselectedOnKeyPressed = false;

    +    Runnable deselectMenusOnKeyPressed = new Runnable() {
    +        public void run() {
    +            /*
    +             ** check that this menubar's container has contents,
    +             ** and that the first item is a MenuButton....
    +             ** otherwise the transfer is off!
    +             */
    +            menuDeselectedOnKeyPressed = false;
    +            if (container.getChildren().size() > 0) {
    +                if (container.getChildren().get(0) instanceof MenuButton) {
    +//                        container.getChildren().get(0).requestFocus();
    +                    if (focusedMenuIndex >= 0) {
    +                        unSelectMenus();
    +                        menuDeselectedOnKeyPressed = true;
    +                    }
    +                }
    +            }
    +        }
    +    };
    +    Runnable focusOnFirstMenuOnKeyReleased = new Runnable() {
    +        public void run() {
    +            /*
    +             ** check that this menubar's container has contents,
    +             ** and that the first item is a MenuButton....
    +             ** otherwise the transfer is off!
    +             */
    +            if (container.getChildren().size() > 0) {
    +                if (container.getChildren().get(0) instanceof MenuButton) {
    +//                        container.getChildren().get(0).requestFocus();
    +                    if (focusedMenuIndex == -1 && !menuDeselectedOnKeyPressed) {
    +                        unSelectMenus();
    +                        menuModeStart(0);
    +                        openMenuButton = ((MenuBarButton) container.getChildren().get(0));
    +                        openMenu = getSkinnable().getMenus().get(0);
    +                        openMenuButton.setHover();
    +                    }
    +                }
    +            }
    +        }
    +    };
    +
         private boolean pendingDismiss = false;

         // For testing purpose only.
    @@ -650,9 +712,23 @@
                 menuButton.textProperty().bind(menu.textProperty());
                 menuButton.graphicProperty().bind(menu.graphicProperty());
                 menuButton.styleProperty().bind(menu.styleProperty());
    +            // patch because MenuButtonSkin.AUTOHIDE is private
    +            final String AUTOHIDE;
    +            {
    +                try {
    +                    Class<?> clazz = MenuButtonSkin.class;
    +//                    System.out.println("fields = " + Arrays.asList(clazz.getDeclaredFields()).toString());
    +                    Field field = clazz.getDeclaredField("AUTOHIDE");
    +                    field.setAccessible(true);
    +                    AUTOHIDE = (String) field.get(this);
    +                    field.setAccessible(false);
    +                } catch (NoSuchFieldException | SecurityException | IllegalAccessException | IllegalArgumentException ex) {
    +                    throw new UnsupportedOperationException(ex);
    +                }
    +            }
                 menuButton.getProperties().addListener((MapChangeListener<Object, Object>) c -> {
    -                 if (c.wasAdded() && MenuButtonSkin.AUTOHIDE.equals(c.getKey())) {
    -                    menuButton.getProperties().remove(MenuButtonSkin.AUTOHIDE);
    +                if (c.wasAdded() && AUTOHIDE.equals(c.getKey())) {
    +                    menuButton.getProperties().remove(AUTOHIDE);
                         menu.hide();
                     }
                 });

以下是我的MenuBar的完整代码:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.stimulus.control;

import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.Skin;

/**
 *
 * @author daniel
 */
public class CustomMenuBar extends MenuBar {

    public CustomMenuBar() {
    }

    public CustomMenuBar(Menu... menus) {
        super(menus);
    }

    @Override
    protected Skin<?> createDefaultSkin() {
        return new MenuBarSkin(this) {

        };
    }

}