Problem solve Get help with specific problems with your technologies, process and projects.

Working with Keyboard input in WPF

Learn more about working with Keyboard input in Windows Presentation Foundation with this tip about Keyboard classes.

The Keyboard class exposes eight events. Each of these events is also implemented as an AttachedEvent on the Keyboard...

class and as a RoutedEvent in the UIElement class. Since UIElement is the base class for nearly any UI element in WPF you have easy access to these events in your XAML.

    <!-- use the Keyboard Attached event or the UIElement event-->
    <TextBox Keyboard.KeyUp='TextBox_KeyUp'
             KeyUp='TextBox_KeyUp' />


In order to receive a keyboard event from the system the element must have the keyboard focus. Only one element may have keyboard focus at a time and you can test whether an element has focus by examining the IsKeyboardFocused property. Use the GotKeyboardFocus/LostKeyboard events to run code when the focus enters/leaves the element. You can push focus to any element with the Focus method.

if (!hasFocusTextbox.IsKeyboardFocused)

Key input

If you are familiar with Windows Forms development you might look for the Keypress event in the WPF documentation. Don't bother, as it is not available in WPF. The KeyUp and KeyDown events and their matching tunneling events (PreviewKeyUp/PreviewKeyDown) are the main keyboard events that are used to capture key data. The KeyEventArgs and Keyboard class contain many useful members for working with the current keystroke. Here's a listing of some of the KeyEventArgs members.

Here is a screenshot showing some of the Keyboard members.

Cataloging key entries
Before going any further, let's take a quick review of the types of key entries that are used in applications.

Textual keys are the keys that indicate the user wants to input a character, for example 'R' or '%'. To complicate matters in some Asian cultures it is necessary to enter a complex key sequence just to insert a single character. In that case the TextInput event is raised at the completion of the key combination.

Action keys (Esc, Home) are keys that are used to invoke an action. Then there are the Helper keys (also known as modifier keys) like Shift, Ctrl and Alt that work in conjunction with other keys. Shift-H and Ctrl-H can have different meaning to the application. Determining whether a user enters a Textual, Helper, Action key or combination of these keys requires learning a few different techniques.

Determine the Textual Key
Determining which key was pressed is easy. Just test the e.Key property against the Key enum values within the KeyUp/KeyDown event procedure.

Determining the Action Key
Action keys, like the Home key are also trivial to resolve.

Combination keystrokes
Looking for combination keys like Shift-S requires a slightly different technique. It is possible to recognize combination keystrokes in a KeyUp/KeyDown procedure. The following example will work.

Getting the Character

Programmers are interested in two types of keyboard data. Sometimes, you just need to know which key was pressed. Other times, you need to know which character was generated by the key entry. These are not the same, pressing the same physical key on the keyboard may result in different characters depending on the computer culture or OEM keyboard settings.

The TextInput/PreviewTextInput events are new in WPF and worthy of a short discussion. These events fire when a key or key combination is pressed, but only if the result produces text output. In other words, pressing ALT-227 (on the NumPad) causes four KeyUp events to fire, one for each number key and one for the ALT key, but only a single TextInput event when the π symbol is shown in the TextBox. That makes the TextInputa event a prime contender for grabbing the typed character. Working in this event means you can stop worrying about trapping individual keys in KeyUp.

Here are a few of the properties of the TextCompositionEventArgs available in the TextInput events.

Many of the controls suppress the TextInput event and raise their own events. The Textbox does this; it raised the TextChanged event internally when processing the TextInput event. Therefore, your code belongs in the PreviewTextInput event if you want an opportunity to access the char data.

Dig Deeper on Software design and development