C# - Detecting if the SHIFT key is held when opening a context menu

后端 未结 5 2066
感情败类
感情败类 2021-01-01 08:24

In my C# application I want to display a context menu, but I want to add special options to the menu if the SHIFT key is being held down when the context menu is opened.

5条回答
  •  再見小時候
    2021-01-01 09:13

    It's actually much simpler than any of that

                if( Keyboard.IsKeyDown(Key.LeftCtrl) || 
                    Keyboard.IsKeyDown(Key.RightCtrl) ||
                    Keyboard.IsKeyDown(Key.LeftAlt) ||
                    Keyboard.IsKeyDown(Key.RightAlt) ||
                    Keyboard.IsKeyDown(Key.LeftShift) ||
                    Keyboard.IsKeyDown(Key.RightShift))
                {
                    /** do something */
                }
    

    Just make sure your project references PresentationCore and WindowsBase

提交回复
热议问题