A modifier key is a key that does not produce an alphanumeric character but can be used in combination with other keys to alter the meaning of those keys. Typical modifier keys in Java look and feel applications are Shift, Control, and Alt.
This section provides recommendations for the use of keyboard operations, which include keyboard shortcuts, mnemonics, and other forms of navigation, selection, and activation that utilize the keyboard instead of the mouse. (See Appendix A for more on keyboard operations.)
A mnemonic is an underlined letter that typically appears in a menu title, menu item, or the text of a button or other component. The underlined letter reminds users how to activate the equivalent command by pressing the Alt key and the character key that corresponds to the underlined letter. For instance, you could use a mnemonic to give keyboard focus to a text area or to activate a command button. A keyboard shortcut (sometimes called an "accelerator") is a keystroke combination (such as Control-A) that activates a menu item from the keyboard even if the relevant menu is not currently displayed.
The keyboard focus (sometimes called "input focus") designates the active window or component where the user's next keystrokes will take effect. Focus typically moves when users click a component with a pointing device or move to the next component using keyboard equivalents. Either way, users designate the window, or component within a window, that receives input. (Many toolbar buttons are exceptions: for instance, a left-alignment button on a toolbar should not take focus away from the text area where the actual work is taking place.)
In the Java look and feel, many components (including command buttons, checkboxes, radio buttons, toggle buttons, list boxes, combo boxes, tabbed panes, editable cells, and tree components) indicate keyboard focus by displaying a rectangular border (blue, in the default color theme).
Figure 69 Keyboard Focus Indicated by Rectangular Border
Figure 70 Keyboard Focus Indicated by Blinking Bar at Insertion Point
Menus indicate focus with a colored background for menu titles or menu items (blue, in the default color theme).
Figure 71 Keyboard Focus Indicated by Colored Background
Split panes and sliders indicate focus by darkening the zoom buttons and slider indicator (blue, in the default color theme) respectively.
Figure 72 Keyboard Focus Indicated by Drag Texture
Keyboard Navigation and Activation
Keyboard navigation enables users to move keyboard focus from one user interface component to another using the keyboard. Navigation does not necessarily affect the selection and does not, by itself, cause activation. Keyboard activation, on the other hand, enables users to cause an action by using the keyboard.In general, pressing the Tab key moves focus through the major components; Shift-Tab moves through the components in the reverse direction. Control-Tab and Control-Shift-Tab work in a similar fashion and are necessary when keyboard focus is in an element that accepts tabs, such as a text area. Arrow keys are often used to move within groups of components--for example, Tab puts focus in a list box and then the arrow keys move focus among the list box items. The Tab key is used to move among checkboxes.
Once an element has focus, pressing the spacebar typically activates its function, such as selecting or deselecting a checkbox. In a list component, pressing Shift-spacebar extends the selection; pressing Control-spacebar toggles the selection state of the current item without affecting any other selections. (Using the up and down arrow keys actually changes the selection in a list component.)
Some components do not need explicit keyboard focus to be operated. For example, users activate the default button in a dialog box by pressing the Enter or Return key. Similarly, scrollbars can be operated from the keyboard using keys such as Page Up and Page Down if focus is anywhere within the scroll pane.
Keyboard navigation can be useful not only for accessibility purposes but also for power users or users who choose alternative input methods like voice input or touch screens.
Common keyboard navigation and activation operations are summarized in the following table.
| 1 With Shift key, reverses direction |
Note — The keyboard operations described in the previous table generally have separate actions for navigation and activation. For the keyboard shortcuts and mnemonics described in the following sections, however, the same action performs both navigation and activation.
Keyboard shortcuts are keystroke combinations that activate a menu item from the keyboard even if the menu for that command is not currently displayed. Keyboard shortcuts usually consist of a modifier key and a character key, like Control-Z, plus a few special keys such as F1 and Delete. Unlike mnemonics, keyboard shortcuts do not post menus; rather, they perform the indicated actions directly. Since all keyboard shortcuts are available at all times, you cannot reuse them as you do mnemonics.
Figure 73 shows an example of keyboard shortcuts and mnemonics on a typical Edit menu.
Figure 73 Edit Menu With Keyboard Shortcuts and Mnemonics
To use a keyboard shortcut in Java look and feel applications, users typically hold down the Control key (and optionally, an additional modifier key, such as Shift) and press the character key that is shown after the menu item; in some cases, they press the single key that corresponds to the shortcut, such as the Delete key in the previous figure. Typing a keyboard shortcut has the same effect as choosing the corresponding menu item. For instance, to undo an action, users can either choose the Undo item from the Edit menu or hold down the Control key and press Z.
The common keyboard shortcuts (in the order of their use in menus) are summarized in the following table. For an alphabetical listing of the shortcuts in this table, see Common Keyboard Shortcuts.
Mnemonics
Mnemonics provide yet another keyboard alternative to the mouse. A mnemonic is an underlined alphanumeric character in a menu title, menu item, or other interface component. It reminds the user how to activate the equivalent command by simultaneously pressing the Alt key and the character key that corresponds to the underlined letter or numeral.Figure 74 File Menu With Mnemonics and Keyboard Shortcuts
When keyboard focus is not in a text element, the Alt modifier is not always required. Menus are an example. For instance, to choose the Exit command from the File menu, the user can hold down the Alt key and press F to post the File menu, release the Alt key, and then press X.
Unlike keyboard shortcuts, mnemonics can be reused from one context to another. Once users have displayed a menu with a keyboard sequence, the subsequent keypress can activate a command only from that menu. Hence, users can press Alt-F to display the File menu and then type A to activate the Save As command, or press Alt-E to display the Edit menu, and then type A to activate the Select All command.
You should provide mnemonics for components within the dialog boxes in your application. In dialog boxes, users must press a modifier key to activate the associated component. For instance, within a dialog box, you might have a mnemonic for the Help button. Once keyboard focus is within the dialog box, users press Alt and then H to activate the Help button.
- Use common mnemonics as they appear in Table 9. (For an alphabetical listing of the mnemonics, see Table 13.)
- If the mnemonic does not appear in the table of common mnemonics, choose the first letter of the menu item. (For instance, choose J for Justify.)
- If the first letter of the menu item conflicts with the mnemonics of other items, choose a prominent consonant. (For instance, the letter S may have already been designated as the mnemonic for the Style command. Therefore, choose the letter Z as the mnemonic for the Size command.)
- If the first letter of the menu item and the prominent consonant conflict with those of other menu items, choose a prominent vowel.
Figure 75 Navigating to a Component Without Associated Text
setMnemonic method can be used to specify mnemonics for buttons, checkboxes, radio buttons, toggle buttons, and menu titles. For components such as text fields, list boxes, and combo boxes that do not have text of their own, mnemonics can be specified on associated labels. The setDisplayedMnemonic method can be used for labels, and the setMnemonic method for menu items. The labelFor property is used to associate the label and its mnemonic with the appropriate component.