Keyboard Input
The target for mouse input is always the element currently under the mouse, or the element that has currently captured the mouse. This doesn't work so well for keyboard input—the user cannot move the keyboard, and it would be inconvenient to need to keep the mouse directly over a text field while typing. Windows therefore uses a different mechanism for directing keyboard input. At any given moment, a particular element is designated as having the focus, meaning that it acts as the target for keyboard input. The user sets the focus by clicking the control in question with the mouse or stylus, or by using navigation keys such as the Tab and arrow keys.
Tip
The UIElement base class
defines an IsFocused property, so
in principle, any user interface element can receive the focus.
However, the Focusable property
determines whether this feature is enabled on any particular element.
By default, this is true for controls, and false for other
elements.
Table 4-2 shows the keyboard input events offered by user interface elements. Most of these items use tunnel and bubble routing for the preview and main events, respectively.
Table 4-2. Keyboard input events
Event | Routing | Meaning |
|---|---|---|
| Tunnel, Bubble | Element received the keyboard focus. |
| Tunnel, Bubble | Element lost the keyboard focus. |
| Bubble | Element received the logical focus. |
| Bubble | Element lost the logical focus. |
|