Skip to content

Commit 7b22608

Browse files
committed
重构自动移动鼠标逻辑
1 parent 0d2dbbe commit 7b22608

File tree

3 files changed

+56
-39
lines changed

3 files changed

+56
-39
lines changed

src/ComputerLock/Platforms/MouseHook.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,19 @@ internal class MouseHook
66
[DllImport("user32.dll")]
77
private static extern int ShowCursor(bool bShow);
88

9+
[DllImport("user32.dll")]
10+
private static extern int SetCursorPos(int x, int y);
11+
12+
[DllImport("user32.dll")]
13+
private static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
14+
15+
public const int MOUSEEVENTF_LEFTDOWN = 0x0002;
16+
public const int MOUSEEVENTF_LEFTUP = 0x0004;
17+
public const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
18+
public const int MOUSEEVENTF_RIGHTUP = 0x0010;
19+
920
private int _cursorCount = 0;
21+
private Random _random = new Random();
1022

1123
/// <summary>
1224
/// 隐藏鼠标光标。
@@ -47,4 +59,17 @@ public void ResetCursorState()
4759
_cursorCount--;
4860
}
4961
}
62+
63+
/// <summary>
64+
/// 移动鼠标并点击
65+
/// </summary>
66+
public void MoveAndClick()
67+
{
68+
var x = _random.Next(0, 100);
69+
var y = _random.Next(0, 100);
70+
71+
var p = new Point(x, y);
72+
SetCursorPos((int)p.X, (int)p.Y);
73+
mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, x, y, 0, 0);
74+
}
5075
}

src/ComputerLock/Services/GlobalLockService.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ internal class GlobalLockService : IGlobalLockService
2121
private readonly IWindowsMessageBox _messageBox;
2222
private readonly IStringLocalizer<Lang> _lang;
2323
public bool IsLocked { get; private set; }
24-
24+
private CancellationTokenSource? _cts;
2525
public GlobalLockService(ILogger logger, AppSettings appSettings, UserActivityMonitor activityMonitor, HotkeyHook hotkeyHook, TaskManagerHook taskManagerHook, MouseHook mouseHook, SystemKeyHook systemKeyHook, IServiceProvider serviceProvider, IWindowsMessageBox messageBox, IStringLocalizer<Lang> lang)
2626
{
2727
_logger = logger;
@@ -131,6 +131,29 @@ public void Lock()
131131
}
132132
}
133133
_systemKeyHook.DisableSystemKey();
134+
135+
if (_appSettings.IsDisableWindowsLock)
136+
{
137+
_cts = new CancellationTokenSource();
138+
_logger.Write("全局锁定 -> 禁用 Windows 锁屏");
139+
Task.Run(async () =>
140+
{
141+
while (true)
142+
{
143+
_logger.Write("全局锁定 -> 移动鼠标,防止 Windows 锁屏");
144+
_mouseHook.MoveAndClick();
145+
try
146+
{
147+
await Task.Delay(TimeSpan.FromSeconds(30), _cts.Token);
148+
}
149+
catch (TaskCanceledException)
150+
{
151+
_logger.Write("全局锁定 -> 鼠标任务释放");
152+
break;
153+
}
154+
}
155+
}, _cts.Token);
156+
}
134157
IsLocked = true;
135158
}
136159

@@ -194,6 +217,13 @@ private void SystemUnlock()
194217
}
195218

196219
_systemKeyHook.Dispose();
220+
221+
if (_cts != null)
222+
{
223+
_cts.Cancel();
224+
_cts.Dispose();
225+
_cts = null;
226+
}
197227
}
198228

199229
public void Dispose()

src/ComputerLock/WindowLockScreen.xaml.cs

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,6 @@ public partial class WindowLockScreen : Window
2020

2121
public event EventHandler<EventArgs>? OnUnlock;
2222

23-
/// <summary>
24-
/// 引用user32.dll动态链接库(windows api),
25-
/// 使用库中定义 API:SetCursorPos
26-
/// </summary>
27-
[DllImport("user32.dll")]
28-
private static extern int SetCursorPos(int x, int y);
29-
/// <summary>
30-
/// 移动鼠标到指定的坐标点
31-
/// </summary>
32-
public void MoveMouseToPoint(Point p)
33-
{
34-
SetCursorPos((int)p.X, (int)p.Y);
35-
}
36-
37-
//点击事件
38-
[DllImport("User32")]
39-
private static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
40-
public const int MOUSEEVENTF_LEFTDOWN = 0x0002;
41-
public const int MOUSEEVENTF_LEFTUP = 0x0004;
42-
public const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
43-
public const int MOUSEEVENTF_RIGHTUP = 0x0010;
44-
4523
public WindowLockScreen(AppSettings appSettings, IStringLocalizer<Lang> lang, ILogger logger)
4624
{
4725
InitializeComponent();
@@ -131,14 +109,6 @@ private void Timer_Tick(object? sender, EventArgs e)
131109
try
132110
{
133111
var time = DateTime.Now;
134-
if (time.Second % 30 == 0)
135-
{
136-
if (_appSettings.IsDisableWindowsLock)
137-
{
138-
_logger.Write("功能屏幕 -> 移动鼠标,防止 Windows 锁屏");
139-
DoMoveMouse();
140-
}
141-
}
142112
if (_appSettings.EnablePasswordBox)
143113
{
144114
if (_appSettings.IsHidePasswordWindow)
@@ -189,15 +159,7 @@ private void TxtPassword_KeyDown(object sender, System.Windows.Input.KeyEventArg
189159
TxtPassword.Password = "";
190160
}
191161
}
192-
private void DoMoveMouse()
193-
{
194-
var random = new Random();
195-
var x = random.Next(0, 100);
196-
var y = random.Next(0, 100);
197162

198-
MoveMouseToPoint(new Point(x, y));
199-
mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, x, y, 0, 0);
200-
}
201163
private void HidePassword()
202164
{
203165
_logger.Write("功能屏幕 -> 准备隐藏密码框");

0 commit comments

Comments
 (0)