C# で複数グローバルホットキーを登録する方法

いくつかのグローバルホットキーを 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イベントを使用してシステム全体の

bannerAds