Test if the Ctrl key is down using C#

后端 未结 6 1250
一整个雨季
一整个雨季 2020-12-01 09:59

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 do

6条回答
  •  南笙
    南笙 (楼主)
    2020-12-01 10:48

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

提交回复
热议问题