简体   繁体   中英

How to test Ctrl key up?

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?

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.

private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    if(e.Modifiers == Keys.Control)
     ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM