Test if the Ctrl key is down using C#

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!");
        }
    }

Leave a Comment