-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindowsKeyboardHookService.cs
More file actions
378 lines (324 loc) · 15.9 KB
/
WindowsKeyboardHookService.cs
File metadata and controls
378 lines (324 loc) · 15.9 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Microsoft.Extensions.Logging;
using SimBlock.Core.Domain.Entities;
using SimBlock.Core.Domain.Enums;
using SimBlock.Core.Domain.Interfaces;
using SimBlock.Presentation.Configuration;
namespace SimBlock.Infrastructure.Windows
{
/// <summary>
/// Windows-specific implementation of keyboard hook service using Win32 API
/// </summary>
public class WindowsKeyboardHookService : IKeyboardHookService
{
private readonly ILogger<WindowsKeyboardHookService> _logger;
private readonly UISettings _uiSettings;
private readonly KeyboardBlockState _state;
private IntPtr _hookId = IntPtr.Zero;
private NativeMethods.LowLevelKeyboardProc _proc;
// Emergency unlock tracking
private int _emergencyUnlockCount = 0;
private DateTime _lastEmergencyKeyPress = DateTime.MinValue;
private const int EMERGENCY_UNLOCK_REQUIRED_PRESSES = 3;
private const int EMERGENCY_UNLOCK_TIMEOUT_MS = 2000; // 2 seconds between presses
// Track modifier key states within the hook
private bool _ctrlPressed = false;
private bool _altPressed = false;
private bool _shiftPressed = false;
public event EventHandler<KeyboardBlockState>? BlockStateChanged;
public event EventHandler<int>? EmergencyUnlockAttempt;
public event EventHandler<KeyboardHookEventArgs>? KeyEvent;
public bool IsHookInstalled => _hookId != IntPtr.Zero;
public KeyboardBlockState CurrentState => _state;
public WindowsKeyboardHookService(ILogger<WindowsKeyboardHookService> logger, UISettings uiSettings)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_uiSettings = uiSettings ?? throw new ArgumentNullException(nameof(uiSettings));
_state = new KeyboardBlockState();
_proc = HookCallback;
}
public Task InstallHookAsync()
{
if (_hookId != IntPtr.Zero)
{
_logger.LogWarning("Hook is already installed");
return Task.CompletedTask;
}
_logger.LogInformation("Installing keyboard hook...");
// For low-level keyboard hooks (WH_KEYBOARD_LL) the hMod parameter MUST be NULL (IntPtr.Zero)
_hookId = NativeMethods.SetWindowsHookEx(
NativeMethods.WH_KEYBOARD_LL,
_proc,
IntPtr.Zero,
0);
if (_hookId == IntPtr.Zero)
{
int error = Marshal.GetLastWin32Error();
_logger.LogError("Failed to install keyboard hook. Error code: {ErrorCode}", error);
throw new InvalidOperationException($"Failed to install keyboard hook. Error code: {error}");
}
_logger.LogInformation("Keyboard hook installed successfully");
return Task.CompletedTask;
}
public Task UninstallHookAsync()
{
if (_hookId == IntPtr.Zero)
{
_logger.LogWarning("Hook is not installed");
return Task.CompletedTask;
}
_logger.LogInformation("Uninstalling keyboard hook...");
bool result = NativeMethods.UnhookWindowsHookEx(_hookId);
if (!result)
{
int error = Marshal.GetLastWin32Error();
_logger.LogError("Failed to uninstall keyboard hook. Error code: {ErrorCode}", error);
}
else
{
_logger.LogInformation("Keyboard hook uninstalled successfully");
}
_hookId = IntPtr.Zero;
return Task.CompletedTask;
}
public Task SetBlockingAsync(bool shouldBlock, string? reason = null)
{
return Task.Run(() =>
{
_logger.LogInformation("Setting keyboard blocking to {ShouldBlock}. Reason: {Reason}",
shouldBlock, reason ?? "Not specified");
_state.SetBlocked(shouldBlock, reason);
BlockStateChanged?.Invoke(this, _state);
});
}
public Task ToggleBlockingAsync(string? reason = null)
{
return Task.Run(() =>
{
_logger.LogInformation("Toggling keyboard blocking. Current state: {CurrentState}. Reason: {Reason}",
_state.IsBlocked, reason ?? "Not specified");
_state.Toggle(reason);
BlockStateChanged?.Invoke(this, _state);
});
}
/// <summary>
/// Sets the keyboard blocking to simple mode
/// </summary>
public Task SetSimpleModeAsync(string? reason = null)
{
return Task.Run(() =>
{
_logger.LogInformation("Setting keyboard blocking to simple mode. Reason: {Reason}",
reason ?? "Not specified");
_state.SetSimpleMode(reason);
BlockStateChanged?.Invoke(this, _state);
});
}
/// <summary>
/// Sets the keyboard blocking to advanced mode with specific configuration
/// </summary>
public Task SetAdvancedModeAsync(AdvancedKeyboardConfiguration config, string? reason = null)
{
return Task.Run(() =>
{
_logger.LogInformation("Setting keyboard blocking to advanced mode. Reason: {Reason}",
reason ?? "Not specified");
_state.SetAdvancedMode(config, reason);
BlockStateChanged?.Invoke(this, _state);
});
}
/// <summary>
/// Sets the keyboard blocking to select mode with specific configuration
/// </summary>
public Task SetSelectModeAsync(AdvancedKeyboardConfiguration config, string? reason = null)
{
return Task.Run(() =>
{
_logger.LogInformation("Setting keyboard blocking to select mode. Reason: {Reason}",
reason ?? "Not specified");
_state.SetSelectMode(config, reason);
BlockStateChanged?.Invoke(this, _state);
});
}
private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
{
var kbStruct = Marshal.PtrToStructure<NativeMethods.KBDLLHOOKSTRUCT>(lParam);
int message = wParam.ToInt32();
bool isInjected = (kbStruct.flags & NativeMethods.LLKHF_INJECTED) != 0;
// Log all key events when debugging emergency unlock
if ((Keys)kbStruct.vkCode == Keys.U ||
(Keys)kbStruct.vkCode == Keys.ControlKey ||
(Keys)kbStruct.vkCode == Keys.Menu)
{
_logger.LogInformation("Keyboard hook received key: {Key} (vkCode: {VkCode}), Message: {Message}, IsBlocked: {IsBlocked}, Mode: {Mode}",
(Keys)kbStruct.vkCode, kbStruct.vkCode, message, _state.IsBlocked, _state.Mode);
}
if (isInjected)
{
return NativeMethods.CallNextHookEx(_hookId, nCode, wParam, lParam);
}
TrackModifierKeys(kbStruct.vkCode, message);
// Raise per-key event for listeners (e.g., macro recording)
try
{
var isKeyDown = message == NativeMethods.WM_KEYDOWN || message == NativeMethods.WM_SYSKEYDOWN;
var isKeyUp = message == NativeMethods.WM_KEYUP || message == NativeMethods.WM_SYSKEYUP;
var args = new KeyboardHookEventArgs
{
Key = (Keys)kbStruct.vkCode,
VkCode = kbStruct.vkCode,
Message = message,
IsKeyDown = isKeyDown,
IsKeyUp = isKeyUp,
Ctrl = _ctrlPressed,
Alt = _altPressed,
Shift = _shiftPressed,
Timestamp = DateTime.UtcNow
};
KeyEvent?.Invoke(this, args);
}
catch (Exception ex)
{
_logger.LogDebug(ex, "Error raising KeyEvent");
}
// Always check for emergency unlock combination (works for both keyboard and mouse blocking)
// Only trigger on key down events (WM_KEYDOWN = 0x0100)
if (message == NativeMethods.WM_KEYDOWN && IsEmergencyUnlockCombination(kbStruct.vkCode))
{
_logger.LogInformation("Emergency unlock combination detected in keyboard hook");
HandleEmergencyUnlock();
// If keyboard is blocked, prevent this key from reaching applications
if (_state.IsBlocked)
{
return (IntPtr)1; // Block this key press to prevent it from reaching applications
}
// If only mouse is blocked, allow the key to pass through but still handle emergency unlock
}
// Check if we should block the key using the new advanced blocking logic
if (_state.IsKeyBlocked((Keys)kbStruct.vkCode))
{
// Block the key based on current mode and configuration
_logger.LogDebug("Blocking keyboard input for key: {Key} (Mode: {Mode})",
(Keys)kbStruct.vkCode, _state.Mode);
return (IntPtr)1; // Return non-zero to suppress the key
}
}
// Allow the key to pass through
return NativeMethods.CallNextHookEx(_hookId, nCode, wParam, lParam);
}
private void HandleEmergencyUnlock()
{
try
{
var now = DateTime.Now;
var timeSinceLastPress = now - _lastEmergencyKeyPress;
// Reset counter if too much time has passed
if (timeSinceLastPress.TotalMilliseconds > EMERGENCY_UNLOCK_TIMEOUT_MS)
{
_emergencyUnlockCount = 0;
}
_emergencyUnlockCount++;
_lastEmergencyKeyPress = now;
_logger.LogInformation("Emergency unlock attempt {Count}/{Required}",
_emergencyUnlockCount, EMERGENCY_UNLOCK_REQUIRED_PRESSES);
// Notify UI about emergency unlock attempt
EmergencyUnlockAttempt?.Invoke(this, _emergencyUnlockCount);
// Check if we've reached the required number of presses
if (_emergencyUnlockCount >= EMERGENCY_UNLOCK_REQUIRED_PRESSES)
{
_logger.LogWarning("Emergency unlock activated! Keyboard will be unlocked.");
// Reset counter
_emergencyUnlockCount = 0;
// Unlock the keyboard
_ = SetBlockingAsync(false, "Emergency unlock (3x Ctrl+Alt+U)");
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error during emergency unlock");
}
}
private void TrackModifierKeys(uint vkCode, int message)
{
try
{
bool isKeyDown = message == NativeMethods.WM_KEYDOWN || message == NativeMethods.WM_SYSKEYDOWN;
bool isKeyUp = message == NativeMethods.WM_KEYUP || message == NativeMethods.WM_SYSKEYUP;
// Track Control key state
if (vkCode == NativeMethods.VK_LCONTROL || vkCode == NativeMethods.VK_RCONTROL || vkCode == NativeMethods.VK_CONTROL)
{
if (isKeyDown)
_ctrlPressed = true;
else if (isKeyUp)
_ctrlPressed = false;
}
// Track Alt key state
if (vkCode == NativeMethods.VK_LMENU || vkCode == NativeMethods.VK_RMENU || vkCode == NativeMethods.VK_MENU)
{
if (isKeyDown)
_altPressed = true;
else if (isKeyUp)
_altPressed = false;
}
// Track Shift key state
if (vkCode == NativeMethods.VK_LSHIFT || vkCode == NativeMethods.VK_RSHIFT || vkCode == NativeMethods.VK_SHIFT)
{
if (isKeyDown)
_shiftPressed = true;
else if (isKeyUp)
_shiftPressed = false;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error tracking modifier keys");
}
}
private bool IsEmergencyUnlockCombination(uint vkCode)
{
try
{
// Convert Keys enum to virtual key code
uint configuredKeyCode = (uint)_uiSettings.EmergencyUnlockKey;
// Check if it's the configured emergency unlock key
if (vkCode == configuredKeyCode)
{
// Check if the required modifiers are pressed
bool ctrlMatch = !_uiSettings.EmergencyUnlockRequiresCtrl || _ctrlPressed;
bool altMatch = !_uiSettings.EmergencyUnlockRequiresAlt || _altPressed;
bool shiftMatch = !_uiSettings.EmergencyUnlockRequiresShift || _shiftPressed;
// Ensure at least one modifier is required and pressed
// Fix: Check if ANY required modifier is being used, not just if it's pressed
bool hasAnyRequiredModifier = _uiSettings.EmergencyUnlockRequiresCtrl ||
_uiSettings.EmergencyUnlockRequiresAlt ||
_uiSettings.EmergencyUnlockRequiresShift;
bool hasRequiredModifiers = !hasAnyRequiredModifier || // If no modifiers required, always true
(_uiSettings.EmergencyUnlockRequiresCtrl && _ctrlPressed) ||
(_uiSettings.EmergencyUnlockRequiresAlt && _altPressed) ||
(_uiSettings.EmergencyUnlockRequiresShift && _shiftPressed);
// Debug logging
_logger.LogInformation("Emergency unlock key check - Key: {Key} (vkCode: {VkCode}, configuredCode: {ConfiguredCode}), " +
"Ctrl: {CtrlPressed}/{CtrlRequired}, Alt: {AltPressed}/{AltRequired}, Shift: {ShiftPressed}/{ShiftRequired}, " +
"HasRequiredModifiers: {HasMods}",
_uiSettings.EmergencyUnlockKey, vkCode, configuredKeyCode,
_ctrlPressed, _uiSettings.EmergencyUnlockRequiresCtrl,
_altPressed, _uiSettings.EmergencyUnlockRequiresAlt,
_shiftPressed, _uiSettings.EmergencyUnlockRequiresShift,
hasRequiredModifiers);
bool result = ctrlMatch && altMatch && shiftMatch && hasRequiredModifiers;
_logger.LogInformation("Emergency unlock combination check result: {Result}", result);
return result;
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error checking emergency unlock combination");
}
return false;
}
}
}