How to test Ctrl key up?

c#, winforms

Solution

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.

Problem

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?

Original source