Whats the difference between KeyEventArgs.systemKey
and KeyEventArgs.Key
? Is it fine to trap key press event in WPF Usercontrol class as shown below.
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if(e.SystemKey == Key.LeftAlt || e.SystemKey == Key.LeftCtrl || e.SystemKey == Key.RightAlt)
{
this.Focus();
CloseAnyOpenPopups();
}
}
Thanks
Because the Alt key will be handled by the system using e.SystemKey
is the only possibility to find out if Alt was pressed. The property Key
would just return Key.System
.
To make sure you always get the right key you could use this expression:
Key key = (e.Key == Key.System ? e.SystemKey : e.Key);
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