问题
I want to set an accelerator to a JMenuItem.
Right now I am setting it like this
openFile.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
and it's working but I don't want ctrl+o as accelerator. I want 'space' as accelerator but I didn't find any method by which I can generate a KeyStroke corresponding to 'space'.
KeyStroke.getStroke()
either takes a char or (int, int). I didn't find any char corresponding to space also.
回答1:
..didn't find any char corresponding to space also.
KeyEvent.VK_SPACE
I would not be surprised if Swing ignores it, since ' '
is an unusual & hard to see accelerator.
回答2:
Most UI delegates render a KeyEvent.VK_SPACE
accelerator using something like the METRICAL TETRASEME
: ⏘ (U+23D8). For example, an Action might include these lines:
static final int MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, MASK));
来源:https://stackoverflow.com/questions/10160676/java-add-accelerator-to-a-jmenuitem