Skip to content

Commit 3a1369a

Browse files
committed
重构钩子服务
1 parent 1cdbe6b commit 3a1369a

File tree

4 files changed

+94
-28
lines changed

4 files changed

+94
-28
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Diagnostics;
2+
3+
namespace ComputerLock.Platforms;
4+
5+
/// <summary>
6+
/// Windows 系统输入钩子基类
7+
/// </summary>
8+
internal abstract class WindowsInputHook : IDisposable
9+
{
10+
protected int _hookId;
11+
protected readonly WinApi.HookDelegate _hookCallback;
12+
13+
protected WindowsInputHook()
14+
{
15+
_hookCallback = HookCallback;
16+
}
17+
18+
protected abstract int HookType { get; }
19+
20+
protected abstract int HookCallback(int nCode, int wParam, IntPtr lParam);
21+
22+
/// <summary>
23+
/// 安装系统钩子
24+
/// </summary>
25+
public void InstallHook()
26+
{
27+
using Process curProcess = Process.GetCurrentProcess();
28+
string? moduleName = curProcess.MainModule?.ModuleName;
29+
if (moduleName == null)
30+
{
31+
return;
32+
}
33+
_hookId = WinApi.SetWindowsHookEx(HookType, _hookCallback, WinApi.GetModuleHandle(moduleName), 0);
34+
}
35+
36+
public void Dispose()
37+
{
38+
WinApi.UnhookWindowsHookEx(_hookId);
39+
}
40+
}

src/ComputerLock/Platforms/MouseHook.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
1-
namespace ComputerLock.Platforms;
2-
internal class MouseHook
1+
using System.Runtime.InteropServices;
2+
3+
namespace ComputerLock.Platforms;
4+
internal class MouseHook : WindowsInputHook
35
{
46
private int _cursorCount = 0;
57
private Random _random = new Random();
8+
private bool _isAutoInput;
9+
protected override int HookType => WinApi.WH_MOUSE_LL;
10+
11+
public event EventHandler? OnUserInput;
12+
protected override int HookCallback(int nCode, int wParam, IntPtr lParam)
13+
{
14+
if (nCode >= 0)
15+
{
16+
// 处理鼠标事件
17+
HandleMouseEvent(wParam);
18+
}
19+
return WinApi.CallNextHookEx(_hookId, nCode, wParam, lParam);
20+
}
621

722
/// <summary>
823
/// 隐藏鼠标光标。
@@ -49,11 +64,31 @@ public void ResetCursorState()
4964
/// </summary>
5065
public void MoveAndClick()
5166
{
67+
_isAutoInput = true;
5268
var x = _random.Next(0, 100);
5369
var y = _random.Next(0, 100);
5470

5571
var p = new Point(x, y);
5672
WinApi.SetCursorPos((int)p.X, (int)p.Y);
5773
WinApi.mouse_event(WinApi.MOUSEEVENTF_RIGHTDOWN | WinApi.MOUSEEVENTF_RIGHTUP, x, y, 0, 0);
74+
_isAutoInput = false;
75+
}
76+
77+
/// <summary>
78+
/// 处理鼠标事件
79+
/// </summary>
80+
private void HandleMouseEvent(int wParam)
81+
{
82+
if (_isAutoInput)
83+
{
84+
return;
85+
}
86+
87+
// 只处理鼠标按下和释放事件
88+
if (wParam == WinApi.WM_LBUTTONDOWN || wParam == WinApi.WM_LBUTTONUP ||
89+
wParam == WinApi.WM_RBUTTONDOWN || wParam == WinApi.WM_RBUTTONUP)
90+
{
91+
OnUserInput?.Invoke(this, EventArgs.Empty);
92+
}
5893
}
5994
}

src/ComputerLock/Platforms/SystemKeyHook.cs

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,23 @@
1-
using System.Diagnostics;
2-
using System.Runtime.InteropServices;
1+
using System.Runtime.InteropServices;
32

43
namespace ComputerLock.Platforms;
5-
internal class SystemKeyHook : IDisposable
4+
/// <summary>
5+
/// 系统按键钩子,用于禁用系统按键
6+
/// </summary>
7+
internal class SystemKeyHook : WindowsInputHook
68
{
7-
private int _hookId;
8-
private readonly WinApi.HookDelegate _hookCallback;
99
private Hotkey? _ignoreHotkey; // 使用单个Hotkey变量来存储需要忽略的快捷键
1010

11-
public SystemKeyHook()
12-
{
13-
_hookCallback = KeyboardHookCallback;
14-
}
11+
protected override int HookType => WinApi.WH_KEYBOARD_LL;
1512

16-
public void DisableSystemKey()
17-
{
18-
using Process curProcess = Process.GetCurrentProcess();
19-
string? moduleName = curProcess.MainModule?.ModuleName;
20-
if (moduleName == null)
21-
{
22-
return;
23-
}
24-
_hookId = WinApi.SetWindowsHookEx(WinApi.WH_KEYBOARD_LL, _hookCallback, WinApi.GetModuleHandle(moduleName), 0);
25-
}
13+
public event EventHandler? OnUserInput;
2614

2715
public void SetIgnoreHotkey(Hotkey hotKey)
2816
{
2917
_ignoreHotkey = hotKey;
3018
}
31-
private int KeyboardHookCallback(int nCode, int wParam, IntPtr lParam)
19+
20+
protected override int HookCallback(int nCode, int wParam, IntPtr lParam)
3221
{
3322
if (nCode >= 0)
3423
{
@@ -38,6 +27,7 @@ private int KeyboardHookCallback(int nCode, int wParam, IntPtr lParam)
3827
{
3928
if (IsSystemKey(vkCode) && (wParam == WinApi.WM_KEYDOWN || wParam == WinApi.WM_SYSKEYDOWN))
4029
{
30+
OnUserInput?.Invoke(this, EventArgs.Empty);
4131
return 1; // 阻止事件传递
4232
}
4333
return WinApi.CallNextHookEx(_hookId, nCode, wParam, lParam); // 其他按键放行
@@ -112,10 +102,4 @@ private bool IsModifierRequired(int vkCode)
112102
}
113103
return false;
114104
}
115-
116-
public void Dispose()
117-
{
118-
_ignoreHotkey = null;
119-
WinApi.UnhookWindowsHookEx(_hookId);
120-
}
121105
}

src/ComputerLock/Platforms/WinApi.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ internal static class WinApi
2424
public const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
2525
public const int MOUSEEVENTF_RIGHTUP = 0x0010;
2626

27+
public const int WH_MOUSE_LL = 14;
28+
public const int WM_MOUSEMOVE = 0x0200;
29+
public const int WM_LBUTTONDOWN = 0x0201;
30+
public const int WM_LBUTTONUP = 0x0202;
31+
public const int WM_RBUTTONDOWN = 0x0204;
32+
public const int WM_RBUTTONUP = 0x0205;
33+
2734
[DllImport("user32.dll")]
2835
public static extern int ShowCursor(bool bShow);
2936

0 commit comments

Comments
 (0)