【问题标题】:replace Windows short cuts for all programs替换所有程序的 Windows 快捷方式
【发布时间】:2015-03-12 21:20:25
【问题描述】:

是否可以让我的覆盖在系统范围内优先,所以即使在运行网络浏览器、文字编辑器或绘图程序时(我的应用仍将在后台运行或作为服务显然)

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

【问题讨论】:

标签: c# service hotkeys


【解决方案1】:

您应该使用 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();
    }
}

【讨论】:

  • GlobalHooker ???严重地? Jeez...我们程序员在命名类时应该更加注意。再想一想:也许他们是故意的。我猜它在“业务”层。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-19
  • 1970-01-01
  • 2021-04-19
  • 1970-01-01
  • 2014-06-22
  • 1970-01-01
相关资源
最近更新 更多