Set Multiple Masks for KeyStroke

895 views Asked by At

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?

1

There are 1 answers

1
trashgod On BEST ANSWER

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