Joshua Goldberg
Joshua Goldberg

Reputation: 5333

How do you refer to the mac command key in the String version of Java's KeyStroke.getKeystroke?

The documentation for KeyStroke.getKeystroke(String) (e.g., getKeyStroke("control DELETE")) does not provide an example of how to access the macintosh command key, and I can't find a reference that lists the spellings of the various words for modifiers like "control" that this function accepts. What is the syntax for the command key?

For reference, here's the documentation for getKeystroke:


Parses a string and returns a KeyStroke. The string must have the following syntax:

<modifiers>* (<typedID> | <pressedReleasedID>)
  modifiers := shift | control | ctrl | meta | alt | altGraph
  typedID := typed <typedKey>
  typedKey := string of length 1 giving Unicode character.
  pressedReleasedID := (pressed | released) key
  key := KeyEvent key code name, i.e. the name following "VK_".

If typed, pressed or released is not specified, pressed is assumed. Here are some examples:

  "INSERT" => getKeyStroke(KeyEvent.VK_INSERT, 0);
  "control DELETE" => getKeyStroke(KeyEvent.VK_DELETE, InputEvent.CTRL_MASK);
  "alt shift X" => getKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK);
  "alt shift released X" => getKeyStroke(KeyEvent.VK_X, InputEvent.ALT_MASK | InputEvent.SHIFT_MASK, true);
  "typed a" => getKeyStroke('a');

Upvotes: 3

Views: 935

Answers (1)

Joshua Goldberg
Joshua Goldberg

Reputation: 5333

I had to go to the source code for AWTKeyStroke getAWTKeyStroke(String s) to see all of the acceptable modifier terms, and then do a little trial and error, to check that the modifier syntax for the command key is "meta".

Upvotes: 6

Related Questions