How can a KeyListener detect key combinations (e.g., ALT + 1 + 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.

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.

1 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. – Hovercraft Full Of Eels Oct 21 at 15:24 You are completely correct. I did not read the question correctly.

– Jonathan Spooner Oct 21 at 15:26 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. – baphomet13 Oct 21 at 15:30 2nd.

Keybindings per one day +1, maybe I miss there JComponent. WHEN_IN_FOCUSED_WINDOW – mKorbel Oct 21 at 17:05 @mKorbel: Sorry, I don't quite get your comment. Please specify what you mean?

– baphomet13 Oct 24 at 9:09.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions