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

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
    }