49 lines
1.3 KiB
C#
49 lines
1.3 KiB
C#
using System;
|
|
using System.Runtime.InteropServices;
|
|
using Microsoft.UI.Xaml;
|
|
|
|
namespace ClipForge
|
|
{
|
|
public class GlobalHotkeyService
|
|
{
|
|
// These two lines talk directly to Windows to register/unregister hotkeys
|
|
[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);
|
|
|
|
// This is the ID we'll use to identify our clip hotkey
|
|
private const int HOTKEY_CLIP = 1;
|
|
|
|
// Alt key modifier
|
|
private const uint MOD_ALT = 0x0001;
|
|
|
|
// F9 key
|
|
private const uint VK_F9 = 0x78;
|
|
|
|
// This is the "event" that fires when the hotkey is pressed
|
|
public event Action? ClipRequested;
|
|
|
|
private IntPtr _hwnd;
|
|
|
|
public void Initialize(IntPtr hwnd)
|
|
{
|
|
_hwnd = hwnd;
|
|
RegisterHotKey(_hwnd, HOTKEY_CLIP, MOD_ALT, VK_F9);
|
|
}
|
|
|
|
public void ProcessHotkey(int id)
|
|
{
|
|
if (id == HOTKEY_CLIP)
|
|
{
|
|
ClipRequested?.Invoke();
|
|
}
|
|
}
|
|
|
|
public void Cleanup()
|
|
{
|
|
UnregisterHotKey(_hwnd, HOTKEY_CLIP);
|
|
}
|
|
}
|
|
} |