Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test Ctrl key up?

Tags:

c#

winforms

I can't get the Ctrl key state in the KeyUp event handler as the Ctrl key is released.

Do I have to test the keycode of the event argument?

Is there any other way?

like image 493
Benny Avatar asked Dec 14 '22 02:12

Benny


1 Answers

Wiring an event to the KeyUp event handler will work.

The following code will trigger when the Ctrl key is released:

private void Form1_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.ControlKey)
    {
        MessageBox.Show("Control key up");
    }
}


If you want to test if the Ctrl was pressed in combination with another keystroke, for example: Ctrl+F1 then the following code snippet might apply:

private void Form1_KeyUp(object sender, KeyEventArgs e)
{
    if (e.Control && e.KeyCode == Keys.F1)
    {
        MessageBox.Show("Control + F1 key up");
    }
}


Side note: You might have to enable KeyPreview on the form in order to catch all control KeyUp events in a single location.

like image 99
Philip Fourie Avatar answered Dec 16 '22 16:12

Philip Fourie