Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 312
Martin Gaviria Avatar asked Dec 21 '25 05:12

Martin Gaviria


2 Answers

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

if ((e.getModifiers() & InputEvent.SHIFT_MASK) != 0) {
    // Shift is down...
}
like image 123
MadProgrammer Avatar answered Dec 22 '25 18:12

MadProgrammer


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.

like image 21
trashgod Avatar answered Dec 22 '25 19:12

trashgod



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!