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!");
}
}
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;
}
Even this also
private void Control_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (ModifierKeys == Keys.Control)
MessageBox.Show("with CTRL");
}