在我的
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); }