Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 932
s.d Avatar asked Oct 21 '11 15:10

s.d


People also ask

What is the purpose of KeyListener interface?

Interface KeyListener The listener interface for receiving keyboard events (keystrokes). The class that is interested in processing a keyboard event either implements this interface (and all the methods it contains) or extends the abstract KeyAdapter class (overriding only the methods of interest).

Which method of KeyEvent class returns true if the key is an action key?

For key pressed and key released events, the getKeyCode method returns the event's keyCode.


2 Answers

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.

like image 94
Jonathan Spooner Avatar answered Oct 12 '22 19:10

Jonathan Spooner


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.

like image 22
Sasivarnan Avatar answered Oct 12 '22 19:10

Sasivarnan