I have some code which is (supposed to be) capturing keystrokes. The top level window has a
Keyboard.PreviewKeyDown="Window_PreviewKeyDown"
clause and the backing CS file contains:
private void Window_PreviewKeyDown(object sender, KeyEventArgs e) { if (e.KeyboardDevice.Modifiers == ModifierKeys.Control) { switch (e.Key) { case Key.L: btnPrev_Click(sender, new RoutedEventArgs()); e.Handled = true; break; case Key.R: btnNext_Click(sender, new RoutedEventArgs()); e.Handled = true; break; } } }
Now that works fine, both CTRLL and CTRLR call the relevant functions.
As soon as I change the modifer check to use ModifierKeys.Alt
, it stops working. In other words, neither ALTL and ALTR call the functions.
What am I missing here?
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With