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.
Thank you
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.
Thank you
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());
}
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" );
}