How can a KeyListener detect key combinations (e.g., ALT + 1 + 1)

54,339

Solution 1

You should not use KeyListener for this type of interaction. Instead use key bindings, which you can read about in the Java Tutorial. Then you can use the InputEvent mask to represent when the various modifier keys are depresed. For example:

// Component that you want listening to your key
JComponent component = ...;
component.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE,
                            java.awt.event.InputEvent.CTRL_DOWN_MASK),
                    "actionMapKey");
component.getActionMap().put("actionMapKey",
                     someAction);

See the javadoc for KeyStroke for the different codes you can use while getting the KeyStroke. These modifiers can be OR'ed together to represent various combinations of keys. Such as

KeyStroke.getKeyStroke(KeyEvent.VK_SPACE,
                       java.awt.event.InputEvent.CTRL_DOWN_MASK
                       | java.awt.event.InputEvent.SHIFT_DOWN_MASK)

To represent when the Ctrl + Shift keys were depressed.

Edit: As has been pointed out, this does not answer you question but instead should just be taken as some good advice.

Solution 2

You can use KeyListener for this purpose by combining certain things. Look at the following example, which should be placed in an overridden keyPressed(KeyEvent e) method.

if (e.isControlDown() && e.getKeyChar() != 'a' && e.getKeyCode() == 65) {
        System.out.println("Select All"); 
}

The string Select All will be displayed when Ctrl + a is pressed. The method e.isControlDown() checks whether the Ctrl key is pressed or not. Similarly, the Alt key combinations can be done with the same method by using e.isAltDown() method.

The logic behind this is, e.getKeyChar() returns the character of the key presses & e.getKeyCode() returns its ASCII code. When Ctrl is pressed and hold, the e.getKeyChar() won't return a and e.getKeyCode() will be the same 65. Hope you understand this. Feel free to ask more.

Solution 3

ALT + 1 + 0 (or "ALT+10" as it could be described in a Help file or similar)

seems to clash with (from one of your comments):

So for example if the user wants to change data in column 11 (which would be called "10"), s/he'd press ALT + 1 + [lets go of both ALT and 1] 0.

Assuming that ALT+10 means 'Pressing ALT, pressing and releasing 1, pressing and releasing 0, releasing ALT' I propose trying this:

In keyPressed, listening for the ALT key, activate a boolean flag, isAltPressed, and create a buffer to hold key presses that occur (a string, say). In keyTyped, if isAltPressed is active, append key codes to the buffer. In keyReleased, listening for ALT again, open a conditional querying the buffer and executing actions.

    public void keyPressed (KeyEvent e){
        if (e.getKeyCode() == KeyEvent.VK_ALT){
        buffer = ""; //declared globally
        isAltPressed = true; } //declared globally
    }

    public void keyTyped (KeyEvent e){
        if (isAltPressed)
            buffer.append (e.getKeyChar());
    }

    public void keyReleased (KeyEvent e){
        if (e.getKeyCode() == KeyEvent.VK_ALT){
            isAltPressed = false;
            if (buffer.equals (4948)) //for pressing "1" and then "0"
                doAction();
            else if (buffer.equals(...))
                doOtherAction();
            ...
        }//if alt
    }
Share:
54,339
s.d
Author by

s.d

Updated on July 14, 2022

Comments

  • s.d
    s.d almost 2 years

    How can I let my custom KeyListener listen for combinations of ALT (or CTRL for that matter) + more than one other key?

    Assume I have 11 different actions I want the application to do, depending on a combination of keys pressed. ALT + 0 - ALT + 9 obviously don't pose any problems, whereas for ALT + 1 + 0 (or "ALT+10" as it could be described in a Help file or similar) I cannot find a good solution anywhere on the web (or in my head). I'm not convinced that this solution with a timer is the only possible way.

    Thanks a million in advance for any suggestions!

    Edit: Actions 0-9 + action 10 = 11 actions. Thanks @X-Zero.

  • Hovercraft Full Of Eels
    Hovercraft Full Of Eels over 12 years
    I agree 100% with your recommending key bindings and have up-voted your answer because of this, but this answer doesn't address his main issue: capturing alt + two different key presses combined.
  • Jonathan Spooner
    Jonathan Spooner over 12 years
    You are completely correct. I did not read the question correctly.
  • s.d
    s.d over 12 years
    Thanks @Jonathan Spooner for recommending key bindings anyway. This makes a lot of sense, especially since it solves the focus problem a lot of people usually have with KeyListeners.
  • mKorbel
    mKorbel over 12 years
    2nd. Keybindings per one day +1, maybe I miss there JComponent.WHEN_IN_FOCUSED_WINDOW
  • s.d
    s.d over 12 years
    @mKorbel: Sorry, I don't quite get your comment. Please specify what you mean?
  • mKorbel
    mKorbel over 12 years
    @baphomet13 that's to (Jonathan Spooner)
  • s.d
    s.d over 12 years
    I've accepted this answer, as it points towards what I should use instead of KeyListener, i.e. key bindings.
  • Andrew Thompson
    Andrew Thompson about 9 years
    "As has been pointed out, this does not answer you question but instead should just be taken as some good advice." See Is “Don't do it” a valid answer? Summary: Heck yes!