conjure-os-overlay/Overlay/HotkeyManager.cs
2025-10-10 18:26:49 -04:00

50 lines
1.5 KiB
C#

using System;
using System.Runtime.InteropServices;
using System.Windows.Interop;
namespace Conjure.Arcade.Overlay
{
public class HotkeyManager
{
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private const int HOTKEY_ID = 9000;
private const uint MOD_ALT = 0x0001;
private const uint MOD_CONTROL = 0x0002;
private readonly IntPtr _windowHandle;
private readonly Action _callback;
public HotkeyManager(IntPtr windowHandle, Action callback)
{
_windowHandle = windowHandle;
_callback = callback;
ComponentDispatcher.ThreadPreprocessMessage += OnThreadPreprocessMessage;
}
public void Register()
{
// Register Ctrl+Alt+O as the hotkey
RegisterHotKey(_windowHandle, HOTKEY_ID, MOD_CONTROL | MOD_ALT, 0x4F); // 0x4F is 'O'
}
public void Unregister()
{
UnregisterHotKey(_windowHandle, HOTKEY_ID);
ComponentDispatcher.ThreadPreprocessMessage -= OnThreadPreprocessMessage;
}
private void OnThreadPreprocessMessage(ref MSG msg, ref bool handled)
{
if (msg.message == 0x0312 && msg.wParam.ToInt32() == HOTKEY_ID)
{
_callback();
handled = true;
}
}
}
}