Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Key bindings vs. key listeners in Java

I note that in Java / Swing there seem to be at least two different ways of handling key events:

  • Key Bindings
  • Key Listeners

What are the advantages / disadvantages of each, and when should you prefer one rather than the other?

like image 970
mikera Avatar asked Mar 08 '13 08:03

mikera


People also ask

What is key binding in Java?

Key bindings involve 2 objects InputMap and ActionMap . InputMap maps a user input to an action name, ActionMap maps an action name to an Action . When the user presses a key, the input map is searched for the key and finds an action name, then the action map is searched for the action name and executes the action.

What is key listener in Java?

Interface KeyListenerThe 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).

What are key bindings?

Noun. key binding (plural key bindings) (computing) A key, or key combination, which, when pressed, causes something to happen. synonym ▲ Synonym: keybind. I changed the key binding for pause to the Escape key.

What does keyPressed do in Java?

In Java, there are three types of KeyEvent. The types correspond to pressing a key, releasing a key, and typing a character. The keyPressed method is called when the user presses a key, the keyReleased method is called when the user releases a key, and the keyTyped method is called when the user types a character.


1 Answers

when should you prefer one rather than the other?

Prefer Key Bindings since they were introduced. A KeyListener is a lower level connection with events.

That page for the key bindings covers a lot of the reasons I would tend to use them rather than a KeyListener. It lists many things which are simply 'not available' to a KeyListener. E.G. choices of:

  • WHEN_FOCUSED
  • WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
  • WHEN_IN_FOCUSED_WINDOW

The more I read the linked document, the less I can understand the need to ask the question. E.G.:

An alternative to key bindings is using key listeners. Key listeners have their place as a low-level interface to keyboard input, but for responding to individual keys key bindings are more appropriate and tend to result in more easily maintained code. Key listeners are also difficult if the key binding is to be active when the component doesn't have focus. Some of the advantages of key bindings are they're somewhat self documenting, take the containment hierarchy into account, encourage reusable chunks of code (Action objects), and allow actions to be easily removed, customized, or shared. Also, they make it easy to change the key to which an action is bound. Another advantage of Actions is that they have an enabled state which provides an easy way to disable the action without having to track which component it is attached to.

Text components

As noted by @Robin, text components also have DocumentListener & DocumentFilter which can be added for functionality more suited to text documents. See Text Component Features for more information on document listeners & filters.

like image 75
Andrew Thompson Avatar answered Oct 01 '22 05:10

Andrew Thompson