views:

893

answers:

4

What is the correct way to separate between F1 and i.e. CTRL+F1 respective SHIFT-CTRL+F1 within an KeyListener registered behind i.e. a JButton?

public void keyPressed(KeyEvent event) {
    int key = event.getKeyCode();

    logger.debug("KeyBoard pressed char(" + event.getKeyChar() + ") code (" + key + ")");
}

.. always gives me 112 for F1, 113 for F2 and so on. I understand that I can handle it by taking care of the keyPressed() respective for keyReleased for CTRL / SHIFT / ALT / etc on my own, but I hope that there is a better way.

Many many thanks!!!

+4  A: 

The Solution lies in the parent of KeyEvent (InputEvent)

  1. Use the isAltDown,isControlDown,isShiftDown methods or
  2. Use the getModifiers method
Midhat
Many thanks, that was exactly what I was looking for!
MrG
+1  A: 

Since KeyEvent extends InputEvent, isControlDown, isShiftDown and isAltDown.

bcash
A: 
ForYourOwnGood
Many thanks, but I think that it's a lot easier to fetch the status on demand with isXxxDown() as suggested by Midhat/bcash as to manage the status on my own.
MrG
+1  A: 

KeyEvents are probably a bit low-level when dealing with a Swing widget. Instead go through InputMap and ActionMap.

Tom Hawtin - tackline