.net - Windows 窗体 .NET 中的热键(非全局)

标签 .net winforms hotkeys

在我的 Windows 窗体应用程序中,我希望有一个特殊按钮在每次按下时运行测试。有数十个控件,因此在每个控件中实现它都需要花费太多时间。

有没有办法设置热键,这样无论我在应用程序中做什么,我都可以按下该键,它就会触发我的事件?

最佳答案

您可以在控件或表单中重写ProcessCmdKey并在那里处理您的热键。

来自MSDN :

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);
}

关于.net - Windows 窗体 .NET 中的热键(非全局),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2790913/

相关文章:

windows - 设置 Tortoise SVN Windows 7 热键

objective-c - 注册热键

c# - 在读/写 Access 数据库时获得最少锁定时间的最佳方法

c# - 如何在 c# winforms 中集成 OpenStreetMap

c# - .Net Forms PropertyGrid 在对多个选定对象的属性进行排序时忽略 DisplayNameAttribute

wpf - 如何从 WPF 控件引用父窗体

javascript - JQuery-热键和windows提示问题

asp.net - 让 ASP.NET MVC Web 应用程序加载域中性

.net - 如何在数据库中存储统计信息

c# - DateTime 通过内部刻度进行比较?