​C#怎么注册多个全局热键

avatar
作者
猴君
阅读量:1

要在C#中注册多个全局热键,可以使用Windows API中的RegisterHotKey函数。以下是一个示例代码,演示如何在C#中注册多个全局热键:

using System; using System.Runtime.InteropServices; using System.Windows.Forms;  public class HotKeyManager {     // 导入Windows API中的RegisterHotKey函数     [DllImport("user32.dll")]     private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);      // 导入Windows API中的UnregisterHotKey函数     [DllImport("user32.dll")]     private static extern bool UnregisterHotKey(IntPtr hWnd, int id);      // 定义热键的标识符     private const int HOTKEY_ID_1 = 1;     private const int HOTKEY_ID_2 = 2;      // 定义热键的键值和修饰键     private const int HOTKEY_MODIFIER_1 = 0x0002; // Ctrl键     private const int HOTKEY_MODIFIER_2 = 0x0001; // Alt键     private const int HOTKEY_VK_1 = (int)Keys.F1;     private const int HOTKEY_VK_2 = (int)Keys.F2;      // 定义热键的响应事件     public event EventHandler HotKeyPressed1;     public event EventHandler HotKeyPressed2;      public HotKeyManager(IntPtr hWnd)     {         // 注册第一个热键         RegisterHotKey(hWnd, HOTKEY_ID_1, HOTKEY_MODIFIER_1, HOTKEY_VK_1);          // 注册第二个热键         RegisterHotKey(hWnd, HOTKEY_ID_2, HOTKEY_MODIFIER_2, HOTKEY_VK_2);          // 注册全局热键消息的处理函数         ComponentDispatcher.ThreadPreprocessMessage += ThreadPreprocessMessage;     }      private void ThreadPreprocessMessage(ref MSG msg, ref bool handled)     {         if (handled)             return;          if (msg.message == WM_HOTKEY)         {             switch (msg.wParam.ToInt32())             {                 case HOTKEY_ID_1:                     // 触发第一个热键的事件                     HotKeyPressed1?.Invoke(this, EventArgs.Empty);                     handled = true;                     break;                 case HOTKEY_ID_2:                     // 触发第二个热键的事件                     HotKeyPressed2?.Invoke(this, EventArgs.Empty);                     handled = true;                     break;             }         }     }      public void UnregisterHotKeys(IntPtr hWnd)     {         // 注销热键         UnregisterHotKey(hWnd, HOTKEY_ID_1);         UnregisterHotKey(hWnd, HOTKEY_ID_2);     }      private const int WM_HOTKEY = 0x0312;     [StructLayout(LayoutKind.Sequential)]     private struct MSG     {         public IntPtr hwnd;         public int message;         public IntPtr wParam;         public IntPtr lParam;         public int time;         public POINT pt;     }     [StructLayout(LayoutKind.Sequential)]     private struct POINT     {         public int X;         public int Y;     } } 

使用示例:

public partial class MainForm : Form {     private HotKeyManager hotKeyManager;      public MainForm()     {         InitializeComponent();         hotKeyManager = new HotKeyManager(this.Handle);         hotKeyManager.HotKeyPressed1 += HotKeyManager_HotKeyPressed1;         hotKeyManager.HotKeyPressed2 += HotKeyManager_HotKeyPressed2;     }      private void HotKeyManager_HotKeyPressed1(object sender, EventArgs e)     {         // 第一个热键被触发时的处理逻辑         MessageBox.Show("第一个热键被触发");     }      private void HotKeyManager_HotKeyPressed2(object sender, EventArgs e)     {         // 第二个热键被触发时的处理逻辑         MessageBox.Show("第二个热键被触发");     }      protected override void OnFormClosing(FormClosingEventArgs e)     {         base.OnFormClosing(e);         hotKeyManager.UnregisterHotKeys(this.Handle);     } } 

在上述示例中,我们创建了一个HotKeyManager类来管理热键的注册和处理。通过调用RegisterHotKey函数注册热键,通过调用UnregisterHotKey函数注销热键。在HotKeyManager类中,我们使用ComponentDispatcher.ThreadPreprocessMessage事件来监听全

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!