Chapter 12.3.5 Keyboard Accelerators | Introduction to Programming Using Java
12.3.5 Keyboard Accelerators
In most programs, commonly used menu commands have keyboard equivalents. The user can type the keyboard equivalent instead of selecting the command from the menu, and the result will be exactly the same.
Typically, for example, the “Save” command has a keyboard equivalent to CONTROL-C, and the “Undo” command corresponds to CONTROL-Z. (Under Mac OS, the keyboard equivalents for these commands would probably be META-C and META-Z, where META refers to holding down the “apple” key.) The keyboard equivalents for menu commands are referred to as accelerators.
The class java. swing.KeyStroke is used to represent keystrokes that the user can type on the keyboard. A keystroke consists of pressing a key, possibly while holding down one or more of the modifier keys control, shift, alt, and meta. The KeyStroke class has a static method, get keystrokes(String), that makes it easy to create keystroke objects. For example,
returns a KeyStroke that represents the action of pressing the “S” key while holding down the control key. In addition to “ctrl”, you can use the modifiers “shift”, “alt”, and “meta” in the string that describes the key stroke. You can even combine several modifiers, so that
represents the action of pressing the “Z” key while holding down both the control and the shift keys. When the key stroke involves pressing a character key, the character must appear in the string in upper case form. You can also have key strokes that correspond to non-character keys. The number keys can be referred to as “1”, “2”, etc., while certain special keys have names such as “F1”, “ENTER”, and “LEFT” (for the left arrow key).
The class KeyEvent defines many constants such as VK ENTER, VKLEFT, and VK S. The names that are used for keys in the keystroke description are just these constants with the leading “VK” removed.
There are at least two ways to associate a keyboard accelerator with a menu item. One is to use the setAccelerator() method of the menu item object:
The other technique can be used if the menu item is created from an Action. The action property Action.ACCELERATORKEY can be used to associate a KeyStroke with an Action. When a menu item is created from the action, the keyboard accelerator for the menu item is taken from the value of this property. For example, if redoAction is an Action representing a “Redo” action, then you might say:
or, alternatively, you could simply add the action to a JMenu, editMenu, with editMenu.add(redoAction). (Note, by the way, that accelerators apply only to menu items, not to push buttons. When you create a JButton from an action, the ACCELERATORKEY property of the action is ignored.)
Note that you can use accelerators for JCheckBoxMenuItems and JRadioButtonMenuItems, as well as for simple JMenuItems.
For an example of using keyboard accelerators, see the solution to Exercise 12.2.
By the way, as noted above, in the Macintosh operating system, the meta (or apple) key is usually used for keyboard accelerators instead of the control key. If you would like to make your program more Mac-friendly, you can test whether your program is running under Mac OS and, if so, adapt your accelerators to the Mac OS style. The recommended way to detect Mac OS is to test the value of System.getProperty(“mrj.version”).
This function call happens to return a non-null value under Mac OS but returns null under other operating systems. For example, here is a simple utility routine for making Mac-friendly accelerators:
- Create a KeyStroke that uses the meta key on Mac OS and* the control key on other operating systems.
- @param description a string that describes the keystroke,
- without the “meta” or “ctrl”; for example, “S” or
- “shift Z” or “alt F1”
- @return a keystroke created from the description string
- with either “ctrl ” or “meta ” prepended
SEE MORE:
- Chapter 12.3.6 HTML on Buttons | Introduction to Programming Using Java
- Chapter 12.4 Complex Components and MVC | Model-View-Controller | Introduction to Programming Using Java
- Chapter 12.4.2 Lists and ListModels | Introduction to Program ming Using Java
- Chapter 12.4.4 Documents and Editors | Introduction to Program’ming Using Java
- Chapter 12.5.2 Design of the Program | Introduction to Program’ming Using Java
- Exercises for Chapter 12 | Introduction to Programming’Using Java
- Chapter 5.5.3 – Example: Vehicles | Introduction to Programming Using Java
- Chapter 5.6 – This and Super | Introduction to Programming’Using Java
- Chapter 5.7 – Interfaces | Introduction to Programming’Using Java
8 thoughts on “Chapter 12.3.5 Keyboard Accelerators | Introduction to Programming Using Java”