简体   繁体   English

如何测试Ctrl键?

[英]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. 释放Ctrl键后,我无法在KeyUp事件处理程序中获取Ctrl键状态。

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. 将事件连接到KeyUp事件处理程序将起作用。

The following code will trigger when the Ctrl key is released: 释放Ctrl键时将触发以下代码:

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: 如果您想测试Ctrl是否与其他按键组合使用,例如: Ctrl + F1,则可能会应用以下代码段:

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. 附注:您可能必须在表单上启用KeyPreview才能在单个位置捕获所有控件KeyUp事件。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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