Can a Java application detect 2 keys pressed at the same time?

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.
0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact