I have some code which is (supposed to be) capturing keystrokes. The top level window has a
Keyboard.PreviewKeyDown=\"Window_PreviewKeyDown\"
The trouble is that when Alt is held down your KeyEventArgs has:
Key = Key.System
SystemKey = the real key
so when checking for Alt you need to use e.SystemKey instead of e.Key, like this:
if (e.KeyboardDevice.Modifiers == ModifierKeys.Alt) {
switch (e.SystemKey) {
...
Explanation
Under Windows, the "Alt" key is handled specially. When the Alt key itself is pressed or another key is pressed while the Alt key is held down, it is considered a "System" keypress. "System" keypresses are handled differently than regular keypresses in many ways.
It all starts out when Windows passes the keypress to your application. Normal key down events generate a WM_KEYDOWN, but if the Alt key is pressed it generates a WM_SYSKEYDOWN. By the same token a WM_KEYUP is translated into a WM_SYSKEYUP.
Throughout Windows, including in WPF, the special handling of the Alt key is used with MenuItems, Buttons and Labels that include "access text". For example, if a button has content of "Say _Hi", then presing Alt-H will be treated as a a button click.
When the Alt key is down, letters come in as three pairs of events: KeyDown, KeyUp and TextInput, each with their associated preview versions. The primary differences here are:
In case of an Alt
modifier, e.Key
returns Key.System
and the real key is in e.SystemKey
. You can use the following piece of code to always get the correct pressed key:
Key key = (e.Key == Key.System ? e.SystemKey : e.Key);