-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHotkeyManager.cs
More file actions
46 lines (37 loc) · 1.18 KB
/
HotkeyManager.cs
File metadata and controls
46 lines (37 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace AudioSwitcher;
public class HotkeyManager : IDisposable
{
[DllImport("user32.dll", SetLastError = true)]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
public const uint MOD_ALT = 0x0001;
public const uint MOD_CONTROL = 0x0002;
public const uint MOD_SHIFT = 0x0004;
public const uint MOD_WIN = 0x0008;
public const uint MOD_NOREPEAT = 0x4000;
private readonly IntPtr _handle;
private int _nextId = 1;
public HotkeyManager(IntPtr windowHandle)
{
_handle = windowHandle;
}
public int Register(uint modifiers, Keys key)
{
int id = _nextId++;
bool success = RegisterHotKey(_handle, id, modifiers | MOD_NOREPEAT, (uint)key);
return success ? id : -1;
}
public void Unregister(int id)
{
UnregisterHotKey(_handle, id);
}
public void Dispose()
{
for (int i = 1; i < _nextId; i++)
UnregisterHotKey(_handle, i);
}
}