The first MenuItem
inside my MenuBar
receives focus whenever I press down my AltGr key. This is by no means a wanted behavior—although it appears as though it’s the default behavior offered by the MenuBar
itself.
This gets slightly annoying since I’m on a Swedish keyboard—meaning both the []
and the {}
are called upon using the AltGr key.
I’d like to remove the functionality whereas the first MenuItem
inside the MenuBar
gets activated upon pressing down AltGr on a keyboard.
As usual I’ve been browsing around Stackoverflow in the hope to find an answer—but in vain. It’s honestly not very surprising that no one has had this problem before due to the majority of Stackoverflow not actually using Swedish keyboard layouts.
Perhaps someone has either seen a post like this somewhere—in that case, do mark this as a duplicate—in any other case, either point me and anyone who might come across this question in the right direction, or simply answer this question with a somewhat shallow example.
Looking at MenuBarSkin's constructor, it adds a scene event key handler which focuses the menu if any keypress involving Alt is not consumed by the time it reaches the scene:
// 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();
}
});
I've worked around this by putting an event handler on the main content pane of my window, that looks for the key-pressed event of ALT_GRAPH and consumes it. Since my handler goes before the scene handler, it should prevent the menu-focus behaviour from triggering. Roughly:
tabPane.addEventFilter(KeyEvent.KEY_PRESSED, e -> {
if (e.getCode() == KeyCode.ALT_GRAPH)
{
e.consume();
return;
}
});
I'm not sure if AltGr always shows up as ALT_GRAPH; I believe I have seen it show up as ALT with e.isControlDown() being true, but you could also consume that event if none of your menu shortcuts involve Ctrl+Alt (which I'm guessing they won't, as they would be triggered by AltGr since it maps to Ctrl+Alt on Windows).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With