在我的 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/