Key bindings and key listeners in Java

I note that in Java / Swing there are apparently at least two different ways to handle key events:

What are the advantages / disadvantages of each, and when do you prefer one over the other?

+10
java swing keylistener key-bindings key-events
Mar 08 '13 at 8:52
source share
2 answers

when do you prefer one over the other?

Apply key bindings from the moment they are introduced. A KeyListener is a lower-level connection to events.

This key binding page covers many reasons why I would use them rather than KeyListener . It lists many things that are simply โ€œinaccessibleโ€ to KeyListener . EG. choice:

  • WHEN_FOCUSED
  • WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
  • WHEN_IN_FOCUSED_WINDOW

The more I read a related document, the less I can understand the need to ask a question. For example:.

An alternative to key binding is to use key listeners. Key listeners have their place as a low-level interface for keyboard input, but key bindings are more suitable for responding to individual keys and, as a rule, lead to easier code storage. It is also difficult for key listeners if key binding should be active when the component has no focus. Some of the advantages of key bindings are somewhat self-documentation, consideration for the containment hierarchy, promotion of multiple code fragments ( Action objects) and makes it easy to delete, configure or share actions. In addition, they simplify changing the key to which the action is bound. Another advantage of Actions is that they have an enabled state that provides an easy way to disable an action without having to keep track of which component it is connected to.

Text components

As @Robin noted, text components also have DocumentListener and DocumentFilter , which can be added for functionality more suitable for text documents. See Text Component Functions for more information on document listeners and filters .

+21
Mar 08 '13 at 8:54
source share
  • KeyBindings (high abstraction)



advantages

  • customizable, affordable,

  • designed for simple shortcuts, without unwanted side effects (most of these events are quite simple and can be set)

  • itโ€™s inconvenient to solve any problem with focus in the window (you can also install it, of course, in Java the window should focus on the screen)

  • Swing inside to use KeyBindings, built-in shortcuts, actions, more in @camickr Key bindings (interesting shortcuts and actions are implemented in Swing)

  • the output should consist of a Swing action (same high abstraction in Swing)

limitations

  • unable to override all keyboard keys

  • it is impossible to redefine three or more keys at the same time

  • the code looks very complicated (not quite right, the code sorts in most cases compared to the same code from KeyListener)

  • removed for more information to see commnent by @camickr (requires a Swing timer for repeated actions)

  • impossible to use () with a single method implemented in the API

.

KeyListeners (low level listener)




advantages

  • very easy to use, intuitive

  • the code is very short for two key events

  • no knowledge of Swing, Java required

  • you can override the tree or press several keys (for example,), for very complex key commands, then it does not matter which of them can trigger any individual key events

  • it is possible to program the event.consume () event

  • it is possible to listen to unfinalized internal events from compound JComponents (JComboBox, JSpinner ...)

limitations

  • not available for parts of containers and JComponents

  • (J) The component must be the focus owner and must be focusable.

  • not intended for Swing JComponents

.

AWTEventListener




  • combine all Key and Mouse events, a low-level listener as possible in Java

  • in principle, it makes no sense to use this listener for most (even very complex) graphical interfaces based on Swing

  • I can see that this listener is implemented in AWT based custom components. The necessary peers are taken from the native OS.

  • but there are excellent implementations for AWTEventListener Inactivity of applications and global event listeners from @camickr

.

notice: organizing key events are different platform platforms




+12
Mar 08 '13 at 9:40
source share



All Articles