173

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?

Joan Venge
  • 315,713
  • 212
  • 479
  • 689

6 Answers6

292

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 */ }
kyrylomyr
  • 12,192
  • 8
  • 52
  • 79
136

There's also:

// Have to get this value before opening a dialog, or user will have released the control key
if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
{

}
Chuck Savage
  • 11,775
  • 6
  • 49
  • 69
  • 13
    Much better solution. It also allows you to check all modifiers at once. If you want to handle Ctrl+F, you won't want to handle Ctrl+Shift+F, so you could just check for `(e.Key == Key.F && e.KeyboardDevice.Modifiers == ModifierKeys.Control)` instead of all the other stuff... – ygoe Mar 08 '13 at 19:18
  • 36
    Note that the comparisons in the examples above produces different results! As the ModifierKeys enum has the Flags attribute you can can have any combinations of values in the enum. If you want to catch ONLY the shift key being pressed, use the `Keyboard.Modifiers == ModifierKeys.Shift` statement. If you want to catch the shift key but dont care if other modifiers are pressed at the same time, use the `(Keyboard.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift` or the much better HasFlag syntax `Keyboard.Modifiers.HasFlag(ModifierKeys.Shift)` – Patrik B Aug 27 '13 at 08:48
  • 4
    I could not catch the windows key modifier using this method. (CTRL worked fine.) I was trying to catch `WIN+RightArrow`. – ANeves May 06 '14 at 19:15
  • 1
    @ANeves Interesting, `Keyboard.Modifiers` shows as `None` – Chuck Savage May 07 '14 at 00:34
9
    private bool IsShiftKey { get; set; }

    private void OnPreviewKeyDown(object sender, KeyEventArgs e)
    {
        IsShiftKey = Keyboard.Modifiers == ModifierKeys.Shift ? true : false;

        if ((Key.Oem3 == e.Key || ((IsShiftKey && Key.Oem4 == e.Key) || (IsShiftKey && Key.Oem6 == e.Key) || (IsShiftKey && Key.Oem5 == e.Key)) && (validatorDefn as FormatValidatorDefinition).format == "packedascii"))
        {
           e.Handled = true;
        }
    }
Ciaran Martin
  • 578
  • 4
  • 12
Krushik
  • 97
  • 1
  • 1
5

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.

Josh
  • 730
  • 7
  • 14
5

Partly borrowing from @Josh, and somewhat similar to @Krushik, and also referencing a question about the Difference between KeyEventArgs.systemKey and KeyEventArgs.Key (answering why Josh has to use SystemKey); wherein, with modifier keys (such as Alt), e.Key returns Key.System, and thus the 'real' key is within e.SystemKey.

A way around this, is to first fetch the 'real' key, and then do your conditional:

private void OnPreviewKeyDown(object sender, KeyEventArgs e)
{
    // Fetch the real key.
    var key = e.Key == Key.System ? e.SystemKey : e.Key;

    if ((Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt))
        && key == Key.Return)
    {
        // Execute your code.
    }
}
Elliot
  • 97
  • 1
  • 8
  • Thanks for the answer to the question embedded in my answer ;) I never actually looked it up, good to know! – Josh Aug 19 '20 at 17:32
-1

and as well:

if My.Computer.Keyboard.ShiftKeyDown then ...

My.Computer.Keyboard.CtrlKeyDown

My.Computer.Keyboard.AltKeyDown

Rob
  • 3,488
  • 3
  • 32
  • 27