10

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

Pushkar
  • 275
  • 1
  • 2
  • 9

1 Answers1

8

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);
MatthiasG
  • 4,434
  • 3
  • 27
  • 47