Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between KeyEventArgs.systemKey and KeyEventArgs.Key

Tags:

wpf

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

like image 834
Pushkar Avatar asked May 21 '13 11:05

Pushkar


1 Answers

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);
like image 93
MatthiasG Avatar answered Sep 30 '22 22:09

MatthiasG