3

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.

Vukašin Manojlović
  • 2,645
  • 2
  • 21
  • 26
  • 2
    You'll probably find the answer here: http://stackoverflow.com/questions/5517674/detecting-shift-modifiers-on-mouseevent-generated-from-click-in-swing – poplitea Dec 05 '12 at 01:36

2 Answers2

6

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

if ((e.getModifiers() & InputEvent.SHIFT_MASK) != 0) {
    // Shift is down...
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
4

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.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045