Java - Add accelerator to a JMenuItem

↘锁芯ラ 提交于 2019-12-12 23:00:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!