windows 如何确定在按钮单击期间何时按下控制键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/353773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:43:29  来源:igfitidea点击:

How can I determine when control key is held down during button click

c#windows

提问by rp.

How can I determine when the control key is held down during button click in a C# Windows program? I want one action to take place for Ctrl/Click and a different one for Click.

如何确定在 C# Windows 程序中单击按钮期间按住 Control 键的时间?我希望对 Ctrl/Click 执行一项操作,对 Click 执行另一项操作。

回答by Simon Wilson

And a little bit more:

还有一点:

private void button1_Click ( object sender, EventArgs e )
{           
    if( (ModifierKeys  & Keys.Control) == Keys.Control )
    {
        ControlClickMethod();    
    }
    else
    {
        ClickMethod();
    }
}

private void ControlClickMethod()
{
    MessageBox.Show( "Control is pressed" );
}

private void ClickMethod()
{
    MessageBox.Show ( "Control is not pressed" );
}

回答by lesscode

Assuming WinForms, use Control.ModifierKeys, eg:

假设 WinForms,使用 Control.ModifierKeys,例如:

private void button1_Click(object sender, EventArgs e) {
    MessageBox.Show(Control.ModifierKeys.ToString());
}

Assuming WPF, use Keyboard.Modifiers, eg:

假设 WPF,使用 Keyboard.Modifiers,例如:

private void Button_Click(object sender, RoutedEventArgs e) {
    MessageBox.Show(Keyboard.Modifiers.ToString());
}