66

I have a form that the user can double click on with the mouse and it will do something. Now I want to be able to know if the user is also holding the Ctrl key down as the user double click on the form.

How can I tell if the user is holding the Ctrl key down?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Itay.B
  • 3,991
  • 14
  • 61
  • 96
  • http://stackoverflow.com/questions/973721/c-detecting-if-the-shift-key-is-held-when-opening-a-context-menu This is for the Shiftkey. But the concept is the same. – LiamB Jan 16 '11 at 12:54
  • IMO, It's a messy concept when we have predefined elements in place to aid us here, without resorting to less maintainable 'magic numbers'. – Grant Thomas Jan 16 '11 at 12:59
  • @GrantThomas I'm sure he was referring to the answers in that thread, which provide exactly what you provided above aside from being a different modifier key. – Dan Bechard Nov 06 '12 at 14:24
  • @Dan Possibly, but in hindsight this is just comment-worthy, unless he wishes to copypasta from there and duplicate. – Grant Thomas Nov 06 '12 at 14:26

6 Answers6

116

Using .NET 4 you can use something as simple as:

    private void Control_DoubleClick(object sender, EventArgs e)
    {
        if (ModifierKeys.HasFlag(Keys.Control))
        {
            MessageBox.Show("Ctrl is pressed!");
        }
    }

If you're not using .NET 4, then the availability of Enum.HasFlag is revoked, but to achieve the same result in previous versions:

    private void CustomFormControl_DoubleClick(object sender, EventArgs e)
    {
        if ((ModifierKeys & Keys.Control) == Keys.Control)
        {
            MessageBox.Show("Ctrl is pressed!");
        }
    }
Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
  • Maybe, but if that's not possible then my second example still ought to do the trick. – Grant Thomas Jan 16 '11 at 13:08
  • Enum.HasFlag is about 16 times slower than using the bitwise operation. [stackoverflow.com/questions/7368652](http://stackoverflow.com/questions/7368652/what-is-it-that-makes-enum-hasflag-so-slow) – NET3 Nov 19 '12 at 12:24
  • That's a reasonable consideration, but isn't always one. – Grant Thomas Nov 19 '12 at 13:08
41

Just for completeness... ModifierKeys is a static property of Control, so you can test it even when you are not directly in an event handler:

public static bool IsControlDown()
{
    return (Control.ModifierKeys & Keys.Control) == Keys.Control;
}
Irshad
  • 3,071
  • 5
  • 30
  • 51
Rob
  • 3,315
  • 1
  • 24
  • 35
  • 2
    This is a *Very* important point to note, in the case you are trying to do this check in code that does not reside directly in a form. – eidylon Mar 19 '18 at 17:29
  • Why in particular? – Rob Mar 20 '18 at 18:53
  • In my case, I'm writing a code-behind dll for a plugin in a third party system. Their entire UI framework for their plugins is all web based though, not Windows controls... so even though my code is in a "form", it doesn't have normal access to the usual Windows Forms properties. As a result I cannot just call ModifierKeys; I need to prefix it with the `Control.` qualifier. – eidylon Mar 20 '18 at 19:56
13

This isn't really an answer to the question at hand, but I needed to do this in a console application and the detail was a little different.

I had to add references to WindowsBase and PresentationFramework, and at that point I could do:

if (System.Windows.Input.Keyboard.Modifiers == ModifierKeys.Control)
   blah

Just adding this here in case someone else is doing something similar.

Chris Rae
  • 5,627
  • 2
  • 36
  • 51
7

Even this also

 private void Control_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        if (ModifierKeys == Keys.Control)
            MessageBox.Show("with CTRL");
    }
Echilon
  • 10,064
  • 33
  • 131
  • 217
Javed Akram
  • 15,024
  • 26
  • 81
  • 118
4

This works for me:

 if(Keyboard.IsKeyDown(Key.LeftCtrl))
    {}

And add references to PresentationCore and WindowsBase

bar-tech
  • 41
  • 3
2

The same soneone said above, but comparing as different than zero, which should be a little faster and use less instructions on most architectures:

public static bool IsControlDown()
{
    return (Control.ModifierKeys & Keys.Control) != 0;
}