javaswingmacoskeycodeacceleratorkey

Setting three button accelerator key hot keys in Java (OSX)


I am trying to change a few hot-keys in the java application I am wokring on and it seems that the system I am using may not work with three button key combinations. We currently have a JMenuItem item and we set the hotkey with a call like this:

menuItem.setAccelerator(getAcceleratorKey(accelerator));

And here is the getAcceleratorKey method:

    private KeyStroke getAcceleratorKey(int keyCode) {

    return KeyStroke.getKeyStroke(
                    keyCode,
                    Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(),
                    false);
}

It seems that the keyCode that this method takes as a parameter can only be one key pressed with command. So how then would I do something like Command-shift z for undo? Do I need to use a different class?


Solution

  • KeyStroke.getKeyStroke() takes modifiers as a parameter. A combination of them should give you what you want:

    KeyStroke.getKeyStroke(keyCode, 
      java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.META_MASK);