How to detect modifier key states in WPF?

Joan Venge picture Joan Venge · Apr 22, 2011 · Viewed 85.5k times · Source

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 of a TreeView.

If so how?

Answer

Kyrylo M picture Kyrylo M · Apr 22, 2011

Use class Keyboard. Using Keyboard.IsKeyDown you can check if Control, Shift, Alt is down now.

For Shift:

if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
{ /* Your code */ }

For Control:

if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
{ /* Your code */ }

For Alt:

if (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt))
{ /* Your code */ }