是否可以让我的覆盖优先于系统范围,所以即使在运行Web浏览器,文字编辑器或绘图程序时(我的应用程序仍然会在后台运行或显然作为服务运行)
使用Visual C#2010
我如何覆盖我的代码的示例:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if((keyData == (Keys.Control | Keys.C))
{
//your implementation
return true;
}
else if((keyData == (Keys.Control | Keys.V))
{
//your implementation
return true;
}
else
{
return base.ProcessCmdKey(ref msg, keyData);
}
}
答案 0 :(得分:12)
你应该使用Global Hooks,Global Mouse and Keyboard Hook是一个很好的库,可以简化这个过程。这是一个基于你的问题的例子。
internal class KeyboardHook : IDisposable
{
private readonly KeyboardHookListener _hook = new KeyboardHookListener(new GlobalHooker());
public KeyboardHook()
{
_hook.KeyDown += hook_KeyDown;
_hook.Enabled = true;
}
private void hook_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.C && e.Control)
{
//your implementation
e.SuppressKeyPress = true; //other apps won't receive the key
}
else if (e.KeyCode == Keys.V && e.Control)
{
//your implementation
e.SuppressKeyPress = true; //other apps won't receive the key
}
}
public void Dispose()
{
_hook.Enabled = false;
_hook.Dispose();
}
}
使用示例:
internal static class Program
{
private static void Main()
{
using (new KeyboardHook())
Application.Run();
}
}