-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHotkeyService.cs
More file actions
124 lines (107 loc) · 3.5 KB
/
Copy pathHotkeyService.cs
File metadata and controls
124 lines (107 loc) · 3.5 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
namespace OpenSnap;
/// <summary>
/// Registers global hotkeys (Win+Shift+S, etc.) and dispatches capture
/// actions via events. Must be initialized with a Window handle.
/// </summary>
public sealed class HotkeyService : IDisposable
{
private readonly Window _window;
private HwndSource? _source;
private bool _registered;
// Hotkey IDs
public const int HotkeyCaptureId = 1;
public const int HotkeyActiveWindowId = 2;
// Modifier constants
public const uint MOD_ALT = 0x0001;
public const uint MOD_CONTROL = 0x0002;
public const uint MOD_SHIFT = 0x0004;
public const uint MOD_WIN = 0x0008;
// Default hotkeys
public uint CaptureModifiers { get; set; } = MOD_WIN | MOD_SHIFT;
public uint CaptureKey { get; set; } = 0x53; // S
public uint ActiveWindowModifiers { get; set; } = MOD_WIN | MOD_SHIFT;
public uint ActiveWindowKey { get; set; } = 0x57; // W
public event Action? CaptureFullScreenRequested;
public event Action? CaptureActiveWindowRequested;
public HotkeyService(Window window)
{
_window = window;
}
public void Register()
{
if (_registered) return;
_source = PresentationSource.FromVisual(_window) as HwndSource;
if (_source == null)
{
_window.Loaded += OnWindowLoaded;
return;
}
DoRegister();
}
private void OnWindowLoaded(object sender, RoutedEventArgs e)
{
_window.Loaded -= OnWindowLoaded;
_source = PresentationSource.FromVisual(_window) as HwndSource;
DoRegister();
}
private void DoRegister()
{
if (_source == null) return;
_source.AddHook(WndProc);
RegisterHotkey(HotkeyCaptureId, CaptureModifiers, CaptureKey);
RegisterHotkey(HotkeyActiveWindowId, ActiveWindowModifiers, ActiveWindowKey);
_registered = true;
}
private void RegisterHotkey(int id, uint modifiers, uint key)
{
if (_source == null) return;
NativeMethods.RegisterHotKey(_source.Handle, id, modifiers, key);
}
private void UnregisterHotkey(int id)
{
if (_source == null) return;
NativeMethods.UnregisterHotKey(_source.Handle, id);
}
private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
const int WM_HOTKEY = 0x0312;
if (msg == WM_HOTKEY)
{
int id = wParam.ToInt32();
switch (id)
{
case HotkeyCaptureId:
CaptureFullScreenRequested?.Invoke();
handled = true;
break;
case HotkeyActiveWindowId:
CaptureActiveWindowRequested?.Invoke();
handled = true;
break;
}
}
return IntPtr.Zero;
}
public void Unregister()
{
if (!_registered || _source == null) return;
_source.RemoveHook(WndProc);
UnregisterHotkey(HotkeyCaptureId);
UnregisterHotkey(HotkeyActiveWindowId);
_registered = false;
}
public void Dispose()
{
Unregister();
}
private static class NativeMethods
{
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
}
}