1

How would I go about making a keystone with multiple modifier masks? With the lack of explicitly specifying the command mask, Apple recommends this, for getting the mask:

Toolkit.getDefaultToolkit().getMenuShortcutMask();

In an example:

KeyStroke.getKeyStroke(KeyEvent.VK_N, Toolkit.getDefaultToolkit().getMenuShortcutMask();

On OS X, This would allow me to use this accelerator: Cmd+N/ ⌘N. It shows on the menu bar too:

Menu Bar

However, what if I need to combine modifier masks? Like so: Cmd+Option+N/⌘⌥N. I've tried this:

KeyStroke.getKeyStroke("command option n");

But it doesn't do it. java.awt.Toolkit doesn't seem to give me this option. So how can I add multiple masks to set as the accelerator?

Zizouz212
  • 4,908
  • 5
  • 42
  • 66
  • 1
    I've not tried, but in theory, you should be able to bitwise or the modifiers `Toolkit.getDefaultToolkit().getMenuShortcutMask() | KeyEvent.VK_ALT_DOWN_MASK` or some such – MadProgrammer Jun 24 '15 at 22:20

1 Answers1

1

The toolkit's getMenuShortcutKeyMask() returns InputEvent.META_MASK on Mac OS X. You can add InputEvent.ALT_MASK to that to get ⌥⌘N.

private static final int MASK =
    Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
…
KeyStroke.getKeyStroke(KeyEvent.VK_N, MASK | InputEvent.ALT_MASK)

Starting with this example produces the result pictured:

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • You are genius. You've solved my problems!!!! :D Just for anyone else who comes here, please don't substitute `KeyEvent` for `InputEvent`, you won't get the same result. :) – Zizouz212 Jun 24 '15 at 23:17