I'm trying to make a key pressed listener for my browser app for Command+T to trigger opening a new tab in the same way that most actual browsers do it.
Looked up some possible solutions for this and it looks like I probably have to use KeyCombination
however I can't find anything for the command key. So far, the closest I've found is Control+T.
private KeyCombination newTab = new KeyCodeCombination(KeyCode.T, KeyCombination.CONTROL_DOWN);
...
root.setOnKeyPressed(event -> {
if (newTab.match(event))
tabPane.getTabs().add(new Tab());
});
I know this currently works fine but I really want to use command instead of control because it's much more natural and intuitive.
I believe you're looking for KeyCombination.SHORTCUT_DOWN
.
KeyCombination
...
The
shortcut
modifier is used to represent the modifier key which is used commonly in keyboard shortcuts on the host platform. This is for examplecontrol
on Windows andmeta
(command key) on Mac. By usingshortcut
key modifier developers can create platform independent shortcuts. So the "Shortcut+C" key combination is handled internally as "Ctrl+C" on Windows and "Meta+C" on Mac.
From that documentation, it looks like they refer to "command" as "meta". If you don't want to use the cross-platform SHORTCUT_DOWN
you can use KeyCombination.META_DOWN
instead.
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