热键(不是全球性)的Windows窗体.NET [英] Hotkey (not global) in Windows Forms .NET

查看:126
本文介绍了热键(不是全球性)的Windows窗体.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Windows窗体应用程序,我想创建一个特殊的按钮来运行测试,每次我preSS它。有几十个控件,以便在每一个实现它需要太多的时间。

In my Windows Forms application I would like one special button to run a test everytime I press it. There are dozens of controls so implementing it in each one takes too much time.

有没有一种方法我可以设置一个热键所以,无论我做什么,在应用程序,我可以preSS的关键,它会激发关闭我的事件?

Is there a way I can set a hotkey so, no matter what I am doing in the application, I can press the key, and it will fire off my event?

推荐答案

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

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

从<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.processcmdkey.aspx">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);
}

这篇关于热键(不是全球性)的Windows窗体.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆