我正在尝试为我的浏览器应用程序创建一个按键监听器,以便 Command+T 以与大多数实际浏览器相同的方式触发打开新选项卡。
为此查找了一些可能的解决方案,看起来我可能必须使用 KeyCombination
但是我找不到命令键的任何内容。到目前为止,我发现最接近的是 Control+T。
private KeyCombination newTab = new KeyCodeCombination(KeyCode.T, KeyCombination.CONTROL_DOWN);
...
root.setOnKeyPressed(event -> {
if (newTab.match(event))
tabPane.getTabs().add(new Tab());
});
我知道这目前工作正常,但我真的想使用命令而不是控制,因为它更自然和直观。
最佳答案
我相信您正在寻找 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.
从该文档来看,他们似乎将“命令”称为“元”。如果你不想使用跨平台 SHORTCUT_DOWN
你可以使用 KeyCombination.META_DOWN
相反。
关于Command+T 的 JavaFX 组合键(新标签),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52460857/