Keyboard Focus, Keyboard Navigation, and the Default Button
Keyboard focus determines which window or control will receive information
typed at the keyboard. Windowing systems show which control has focus
by highlighting it. MS Windows uses various means to highlight the control
that has keyboard focus. The 'FOCUS'
mnemonic can be used to force the focus to a particular window or control.
Keyboard navigation allows the
use of standard keys to change keyboard focus. Without keyboard navigation,
it is almost always necessary to use the mouse to operate controls. (The
exception is if hot keys are provided, or if keypresses are detected and
acted upon. In effect, personalized keyboard navigation is being created
when this is done.)
If keyboard navigation is enabled, and a window contains any buttons, there
is always a default button. The
default button is identifiable by a black rectangle drawn around the outside.
Normally the default button is the button with the lowest ID number, but
if another button has focus, that button temporarily becomes the default
button and shows the black rectangle. (If a button is inside a group box,
the black rectangle will not be visible. However, the user can still tell
which button is the default because it will also have keyboard focus.)
If the keyboard navigation flag was specified when a window or child window
was created, key pressed ("t") events for arrow keys and the
tab key will not be reported.
Standard keyboard navigation proceeds as follows:
Key |
Action |
Tab |
Move to the next focusable control. Controls that are disabled or invisible cannot receive keyboard focus, nor can controls that do absolutely nothing, like static text or group boxes. Grouped controls (controls with contiguous control IDs, all created with control creation flag $0020$) are treated as a navigation group; tab goes to the first focusable control in the group. |
BackTab |
Move to the previous focusable control or navigation group. |
Enter |
Operate default button (often OK). |
Space |
Operate the control that has focus. |
Arrow Keys |
Move among grouped controls, unless the control defines its own internal meaning for the arrow key. Arrow key navigation is available between grouped buttons, checkboxes, and radiobuttons, but not between grouped text or list controls. |
Esc |
Operate button with ID=2, if present (usually Cancel). |
Standard Button IDs
If ID 1 id used for OK buttons and ID 2 for Cancel buttons, they will
behave in standard ways when keyboard navigation is enabled. Pressing
Enter will choose OK, provided that no other button has focus. Initially
the OK button will have focus since it has the lowest ID. Pressing Esc
will choose Cancel.
Note that the Enter key will always operate the default button, regardless
of its ID, (provided that keyboard navigation is enabled), but the Esc
key will only work if a button with ID 2 exists.
The title of the button with ID 1 does not have to read "OK",
nor does the one with ID 2 have to read "Cancel", nor does the
code have to behave in any particular way when these buttons are pressed.
The Enter key simply operates the default button, and Esc operates
the button with ID 2, if there is one. ID 1 is suggested for the default
button if it is used as an OK type operation, but this is not the only
way it can be used.
Hot Keys
Whether or not keyboard navigation is enabled, it is possible to assign
hot keys to push buttons, check boxes, and radio buttons and menus. To
do this, specify an ampersand, "&", before the letter or
symbol in the title text which is to be the hot key. It is possible to
have a hot key that is not in the title text. To do this, create the control
with one title, then change titles. Only the initial title determines
the hot key used for a control. Ampersands in subsequent titles merely
appear in the displayed title.
Pressing the hot key character when the window has keyboard focus will
operate the control. It will not work, however, if some other control
in the window has keyboard focus. This is necessary, since some controls
accept text. Under MS Windows, holding down the Alt key while pressing
the hot key will work any time the window has focus, even if another control
currently has focus.
Top-level menu mnemonics can work the same way. For example, to access the File menu, Alt+F could be used. If this is the case, avoid assigning F as the hot key for a control. It may operate both the menu and the control (but the behavior is not guaranteed).