How to detect modifier key states in WPF?

前端 未结 6 1557
粉色の甜心
粉色の甜心 2020-12-04 15:10

Is there some global constructs that I can use whenever I need to access whether the Control, Shift, Alt buttons are down? For instance inside MouseDown event o

6条回答
  •  囚心锁ツ
    2020-12-04 15:34

    This is how I handle it (using PreviewKeyDown), let's say we are looking for Alt + R...

    private void OnPreviewKeyDown(object sender, KeyEventArgs e)
    {
        if ((Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt)
           && e.SystemKey == Key.R)
        {
           //do whatever
        }
    }
    

    Maybe someone can clear up why I had to use e.SystemKey and not just e.Key, maybe due to the modifier? but this has worked flawlessly for me when searching for modifier+key.

提交回复
热议问题