【问题标题】:How to create keyboard shortcut in windows that call function in my app?如何在我的应用程序中调用函数的窗口中创建键盘快捷键?
【发布时间】:2024-05-02 03:45:04
【问题描述】:

如何创建一个使用键盘快捷键执行操作的应用程序(应用程序必须是不可见的)。例如,当用户按下 Ctrl + Alt + W 时显示 MessageBox。

【问题讨论】:

  • 您需要包含有关您要完成的任务的更多信息。技术(winforms、wpf 等)。此外,您的问题似乎暗示它应该是试图挂接键盘的后台任务,但目前尚不清楚。
  • 是的,我希望它在后台工作。和 Winforms

标签: c# keyboard-shortcuts


【解决方案1】:

一种解决方案是使用互操作并使用 Win32 RegisterHotKey API。这是我刚刚放在一起的一个快速而肮脏的例子,所以它没有经过很好的测试,我不确定有没有意外的副作用,但它应该可以工作。

这里首先是一个简单的HotKeyManager,它负责基本的互操作,提供一个隐藏窗口来处理本机 Windows 消息 (WM_HOTKEY),该消息被转换为 .NET 事件HotKeyPressed

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

  public class HotKeyManager
  {
    public static event EventHandler<HotKeyEventArgs> HotKeyPressed;

    public static int RegisterHotKey(Keys key, KeyModifiers modifiers)
    {
      int id = System.Threading.Interlocked.Increment(ref _id);
      RegisterHotKey(_wnd.Handle, id, (uint)modifiers, (uint)key);
      return id;
    }

    public static bool UnregisterHotKey(int id)
    {
      return UnregisterHotKey(_wnd.Handle, id);
    }

    protected static void OnHotKeyPressed(HotKeyEventArgs e)
    {
      if (HotKeyManager.HotKeyPressed != null)
      {
        HotKeyManager.HotKeyPressed(null, e);
      }
    }

    private static MessageWindow _wnd = new MessageWindow();

    private class MessageWindow : Form
    {
      protected override void WndProc(ref Message m)
      {
        if (m.Msg == WM_HOTKEY)
        {
          HotKeyEventArgs e = new HotKeyEventArgs(m.LParam);
          HotKeyManager.OnHotKeyPressed(e);
        }

        base.WndProc(ref m);
      }

      private const int WM_HOTKEY = 0x312;
    }

    [DllImport("user32")]
    private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);

    [DllImport("user32")]
    private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

    private static int _id = 0;
  }


  public class HotKeyEventArgs : EventArgs
  {
    public readonly Keys Key;
    public readonly KeyModifiers Modifiers;

    public HotKeyEventArgs(Keys key, KeyModifiers modifiers)
    {
      this.Key = key;
      this.Modifiers = modifiers;
    }

    public HotKeyEventArgs(IntPtr hotKeyParam)
    {
      uint param = (uint)hotKeyParam.ToInt64();
      Key = (Keys)((param & 0xffff0000) >> 16);
      Modifiers = (KeyModifiers)(param & 0x0000ffff);
    }
  }

  [Flags]
  public enum KeyModifiers
  {
    Alt = 1,
    Control = 2,
    Shift = 4,
    Windows = 8,
    NoRepeat = 0x4000
  }

下面显示了一个简单的 Windows 窗体应用程序,它将隐藏主窗体并响应热键事件。我没有处理应用程序的关闭和热键的注销,你可以处理。

using System;
using System.Windows.Forms;

namespace HotKeyManager
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
      HotKeyManager.RegisterHotKey(Keys.A, KeyModifiers.Alt);
      HotKeyManager.HotKeyPressed += new EventHandler<HotKeyEventArgs>(HotKeyManager_HotKeyPressed);     
    }

    void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e)
    {
      MessageBox.Show("Hello");
    }

    protected override void SetVisibleCore(bool value)
    {      
      // Quick and dirty to keep the main window invisible      
      base.SetVisibleCore(false);
    }
  }
}

【讨论】:

    【解决方案2】:

    将此添加到表单的 KeyPress 事件中:

    if(e.KeyCode == (char)Keys.W && e.Modifiers == Keys.Control && e.Modifiers = Keys.Alt)
    {
        MessageBox.Show("I think this is a homework and that you should study instead of asking for an already cooked up answer on programming websites","Cheater");
    }
    

    【讨论】:

    • 看起来我错过了“应用程序必须是不可见的”部分。