Can a Java application detect 2 keys pressed at the same time?
Created May 4, 2012
Kevin Riff If one of those keys is a meta-key (shift, ctrl, or alt) then use the getModifiers() method to determine which one is pressed. For example, to check for Ctrl+A write:
public void keyPressed(KeyEvent evt) { if (evt.getKeyCode() == 'A' && (evt.getModifiers() & InputEvent.CTRL_MASK) != 0) { ... } }To check for two or more keys that aren't meta-keys you need to track their state. Create a boolean flag for each key you want to track. Then write a keyPressed handler to set the appropriate flag and a keyReleased handler to clear it. Within your keyPressed handler, you can check the state of the other flags and react accordingly.