Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX KeyCombination for Command+T (New Tab)

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.

like image 885
faris Avatar asked Sep 22 '18 21:09

faris


1 Answers

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 example control on Windows and meta (command key) on Mac. By using shortcut 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.

like image 114
Slaw Avatar answered Oct 22 '22 15:10

Slaw