Keymaps

A Keymap contains mappings from KeyStrokes[2] to Actions and provides a variety of methods for accessing and updating these mappings.

The Keymap Interface

One last interface you can use to customize your application without implementing your own L&F is Keymap.

Normally, an L&F defines a set of meaningful keystrokes. For example, Windows users expect Ctrl-C to copy text and Ctrl-V to paste while the Mac uses the Command key instead. Ctrl-Insert and Shift-Insert perform the same tasks for Motif users. These key sequences work as expected in Swing text components because of the Keymap installed by the L&F. This interface lets you change or augment this behavior.

Tip

The Keymap interface has been available to Swing text components since the beginning. SDK 1.3 introduced the more flexible and universal keyboard event system based on InputMaps and ActionMaps (described in Chapter 3). The techniques illustrated in this section remain useful: Keymap support was reimplemented using the new mechanism for backward compatibility. This approach is still quite convenient if all you need is to add a couple of keyboard commands to a text component. Also be sure to learn how to use InputMap and ActionMap directly—they give you more capabilities, in many more situations.

Properties

The Keymap interface defines the properties shown in Table 21-8. The boundAction and boundKeyStrokes properties contain the Actions and KeyStrokes (both part of the javax.swing package) local to the Keymap.

Table 21-8. Keymap ...

Get Java Swing, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.