How to recognise if shift is selected when a button is clicked on an actionPerformed (Java Swing)?

六眼飞鱼酱① 提交于 2019-12-13 15:25:06

问题


I'm trying to create a different actionPerformed when Shift is held down while pressing a JButton, but when I use: event.isShiftDown; my program does not compile because it does't recognise it.


回答1:


Basically you need to bitwise-and the ActionEvent#getModifiers result

if ((e.getModifiers() & InputEvent.SHIFT_MASK) != 0) {
    // Shift is down...
}



回答2:


As an alternative to checking the event modifiers directly, consider using a different Action for each state of the shift key. You can supply the desired mask to the KeyStroke used in your key binding, as outlined here. A related example using getMenuShortcutKeyMask() is shown here.



来源:https://stackoverflow.com/questions/13714966/how-to-recognise-if-shift-is-selected-when-a-button-is-clicked-on-an-actionperfo

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