Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

windows

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.

like image 951
rp. Avatar asked Dec 09 '08 18:12

rp.


People also ask

Why does my laptop say im holding down the Ctrl key?

Recovery: Most of the time, Ctrl+Alt+Del re-sets key status to normal if this is happening. (Then press Esc to exit system screen.) Another method: You can also press stuck key: so if you clearly see that it is Ctrl which got stuck, press and release both left and right Ctrl.

What is hold down Ctrl?

While holding down the Ctrl key you can left-click to select multiple objects or highlight multiple sections of text. For example, in Microsoft Windows you could hold down the Ctrl key and click to select multiple files at once.

How do I turn off Ctrl Lock?

Go to Start / Settings / Control Panel / Accessibility Options /Keyboard Options. b. Turn off CTRL lock if it's on. d.


2 Answers

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" );
}
like image 51
Simon Wilson Avatar answered Oct 08 '22 06:10

Simon Wilson


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());
}
like image 32
lesscode Avatar answered Oct 08 '22 04:10

lesscode