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

后端 未结 2 775
我在风中等你
我在风中等你 2020-12-30 04:41

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 Cli

相关标签:
2条回答
  • 2020-12-30 05:43

    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" );
    }
    
    0 讨论(0)
  • 2020-12-30 05:43

    Assuming WinForms, use Control.ModifierKeys, eg:

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

    Assuming WPF, use Keyboard.Modifiers, eg:

    private void Button_Click(object sender, RoutedEventArgs e) {
        MessageBox.Show(Keyboard.Modifiers.ToString());
    }
    
    0 讨论(0)
提交回复
热议问题