且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

热键(不是全球性)的Windows窗体.NET

更新时间:2023-12-06 13:48:04

您可以覆盖 ProcessCmdKey 和处理您的热键有,无论是在控件或窗体。

You can override ProcessCmdKey and handle your hotkeys there, either in a control or a form.

从MSDN:

ProcessCmdKey方法第一   确定控件是否具有   文本菜单,如果是的话,可以使   的ContextMenu处理命令   键。如果命令键不是菜单   快捷方式和控制有父母,   关键是传递给家长的   ProcessCmdKey方法。净效应   就是命令键冒泡了   控制层次。此外   关键用户pressed,关键数据   还指示,如果有的话,改性剂   关键是pressed在同一时间   钥匙。修饰键包括:   Shift,Ctrl和Alt键。

The ProcessCmdKey method first determines whether the control has a ContextMenu, and if so, enables the ContextMenu to process the command key. If the command key is not a menu shortcut and the control has a parent, the key is passed to the parent's ProcessCmdKey method. The net effect is that command keys are "bubbled" up the control hierarchy. In addition to the key the user pressed, the key data also indicates which, if any, modifier keys were pressed at the same time as the key. Modifier keys include the SHIFT, CTRL, and ALT keys.

例如:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    // if it is a hotkey, return true; otherwise, return false
    switch (keyData)
    {
        case Keys.Control | Keys.C:
            // do something
            return true;
        default:
            break;
    }

    return base.ProcessCmdKey(ref msg, keyData);
}