Skip to content

Commit c63d96c

Browse files
committed
#35 锁屏时注册解锁热键、解锁时释放热键
1 parent b97b4a4 commit c63d96c

File tree

9 files changed

+75
-64
lines changed

9 files changed

+75
-64
lines changed

src/ComputerLock/Components/Settings/LockSettings.razor.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ private void AutoLockChanged(int autoLockMinute)
4747
}
4848
private Task SetLockHotkey(string hotkey)
4949
{
50+
// 检查快捷键是否与解锁快捷键相同
51+
if (hotkey.IsNotEmpty() && hotkey == AppSettings.UnlockHotkeyString)
52+
{
53+
Snackbar.Add(Lang["HotkeyDuplicateError"], Severity.Error);
54+
return Task.CompletedTask;
55+
}
56+
5057
AppSettings.LockHotkeyString = hotkey;
5158
SaveSettings();
5259
RegisterLockHotkey();

src/ComputerLock/Components/Settings/UnlockSettings.razor.cs

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -120,48 +120,25 @@ private void PwdBoxLocationChanged(ScreenLocationEnum location)
120120
SaveSettings();
121121
}
122122

123+
// ========= 热键的注册和注销交给锁定服务处理(仅当锁定时热键才生效)这里只保存设置 ============
123124
private Task SetUnlockHotkey(string hotkey)
124125
{
126+
// 检查快捷键是否与锁屏快捷键相同
127+
if (hotkey.IsNotEmpty() && hotkey == AppSettings.LockHotkeyString)
128+
{
129+
Snackbar.Add(Lang["HotkeyDuplicateError"], Severity.Error);
130+
return Task.CompletedTask;
131+
}
132+
125133
AppSettings.UnlockHotkeyString = hotkey;
126134
SaveSettings();
127-
RegisterUnlockHotkey();
128135
return Task.CompletedTask;
129136
}
130137
private Task ClearUnlockHotkey()
131138
{
132139
AppSettings.UnlockHotkeyString = "";
133140
SaveSettings();
134-
UnregisterUnlockHotkey();
135141
return Task.CompletedTask;
136142
}
137-
138-
public void RegisterUnlockHotkey()
139-
{
140-
try
141-
{
142-
if (AppSettings.UnlockHotkey != null)
143-
{
144-
Logger.Info("注册解锁热键");
145-
HotkeyHook.Register((int)HotkeyType.Unlock, AppSettings.UnlockHotkey);
146-
}
147-
}
148-
catch (Exception ex)
149-
{
150-
Logger.Error($"绑定解锁热键失败", ex);
151-
Snackbar.Add($"{Lang["ExRegistFailed"]}{ex.Message}", Severity.Error);
152-
}
153-
}
154-
public void UnregisterUnlockHotkey()
155-
{
156-
try
157-
{
158-
Logger.Info("释放解锁热键");
159-
HotkeyHook.Unregister((int)HotkeyType.Unlock);
160-
}
161-
catch (Exception ex)
162-
{
163-
Logger.Error($"释放解锁热键失败", ex);
164-
//MessageBoxUtils.ShowError($"取消快捷键失败:{ex.Message}");
165-
}
166-
}
143+
// ================================================================================
167144
}

src/ComputerLock/Platforms/HotkeyHook.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public void Register(int id, Hotkey hotKey)
4747
if (ids.Contains(id))
4848
{
4949
Unregister(id);
50+
ids.Remove(id);
5051
}
5152

5253
var success = WinApi.RegisterHotKey(_nativeWindow.Handle, id, (uint)hotKey.Modifiers, (uint)hotKey.Key);
@@ -64,6 +65,10 @@ public void Register(int id, Hotkey hotKey)
6465
public void Unregister(int id)
6566
{
6667
WinApi.UnregisterHotKey(_nativeWindow.Handle, id);
68+
if (ids.Contains(id))
69+
{
70+
ids.Remove(id);
71+
}
6772
}
6873

6974
public void Dispose()

src/ComputerLock/Platforms/SystemKeyHook.cs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,39 @@ protected override int HookCallback(int nCode, int wParam, IntPtr lParam)
2323
{
2424
int vkCode = Marshal.ReadInt32(lParam);
2525

26+
// 仅在按下阶段进行拦截判断;抬起阶段放行避免影响 WM_HOTKEY 触发
27+
if (!(wParam == WinApi.WM_KEYDOWN || wParam == WinApi.WM_SYSKEYDOWN))
28+
{
29+
return WinApi.CallNextHookEx(_hookId, nCode, wParam, lParam);
30+
}
31+
2632
if (_ignoreHotkey == null)
2733
{
28-
if (IsSystemKey(vkCode) && (wParam == WinApi.WM_KEYDOWN || wParam == WinApi.WM_SYSKEYDOWN))
34+
if (IsSystemKey(vkCode))
2935
{
3036
OnUserInput?.Invoke(this, EventArgs.Empty);
3137
return 1; // 阻止事件传递
3238
}
3339
return WinApi.CallNextHookEx(_hookId, nCode, wParam, lParam); // 其他按键放行
3440
}
3541

36-
if (!IsPartOfIgnoreHotkey(vkCode))
42+
// 属于需要放行的热键组成部分(修饰键或主键)则放行
43+
if (IsPartOfIgnoreHotkey(vkCode))
3744
{
38-
if (IsModifierKey(vkCode) && !IsModifierRequired(vkCode))
39-
{
40-
return 1; // 阻止事件传递
41-
}
42-
else if (vkCode != (int)_ignoreHotkey.Key)
43-
{
44-
return 1; // 阻止事件传递
45-
}
45+
return WinApi.CallNextHookEx(_hookId, nCode, wParam, lParam);
46+
}
47+
48+
// 拦截非必要修饰键
49+
if (IsModifierKey(vkCode) && !IsModifierRequired(vkCode))
50+
{
51+
return 1; // 阻止事件传递
52+
}
53+
54+
// 不是忽略热键
55+
if (vkCode != (int)_ignoreHotkey.Key)
56+
{
57+
return 1; // 阻止事件传递
4658
}
47-
return WinApi.CallNextHookEx(_hookId, nCode, wParam, lParam); // 放行
4859
}
4960
return WinApi.CallNextHookEx(_hookId, nCode, wParam, lParam);
5061
}

src/ComputerLock/Resources/Lang.en.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,4 +432,7 @@
432432
<data name="DisablePasswordBoxTips" xml:space="preserve">
433433
<value>Unlock method: Press [ESC] key or click with mouse to show password box</value>
434434
</data>
435+
<data name="HotkeyDuplicateError" xml:space="preserve">
436+
<value>Lock screen hotkey and unlock hotkey cannot be the same</value>
437+
</data>
435438
</root>

src/ComputerLock/Resources/Lang.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,4 +432,7 @@
432432
<data name="DisablePasswordBoxTips" xml:space="preserve">
433433
<value>当前解锁方式:按 [ESC] 键 或 鼠标点击密码框位置显示密码框</value>
434434
</data>
435+
<data name="HotkeyDuplicateError" xml:space="preserve">
436+
<value>锁屏快捷键和解锁快捷键不能相同</value>
437+
</data>
435438
</root>

src/ComputerLock/Resources/Lang.zh.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,4 +432,7 @@
432432
<data name="DisablePasswordBoxTips" xml:space="preserve">
433433
<value>当前解锁方式:按 [ESC] 键 或 鼠标点击密码框位置显示密码框</value>
434434
</data>
435+
<data name="HotkeyDuplicateError" xml:space="preserve">
436+
<value>锁屏快捷键和解锁快捷键不能相同</value>
437+
</data>
435438
</root>

src/ComputerLock/Services/GlobalLockService.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,16 @@ public void Lock()
215215
if (_appSettings.UnlockHotkey != null)
216216
{
217217
_systemKeyHook.SetIgnoreHotkey(_appSettings.UnlockHotkey);
218+
219+
try
220+
{
221+
_logger.Info("全局锁定 -> 锁定时注册解锁快捷键");
222+
_hotkeyHook.Register((int)HotkeyType.Unlock, _appSettings.UnlockHotkey);
223+
}
224+
catch (Exception ex)
225+
{
226+
_logger.Error("注册解锁快捷键失败", ex);
227+
}
218228
}
219229
}
220230
}
@@ -362,6 +372,19 @@ private void SystemUnlock()
362372
_mouseHook.Dispose();
363373
_systemKeyHook.Dispose();
364374

375+
// 释放解锁快捷键
376+
if (_appSettings.UnlockHotkey != null)
377+
{
378+
try
379+
{
380+
_hotkeyHook.Unregister((int)HotkeyType.Unlock);
381+
}
382+
catch (Exception ex)
383+
{
384+
_logger.Error($"释放解锁热键失败", ex);
385+
}
386+
}
387+
365388
if (_cts != null)
366389
{
367390
_cts.Cancel();

src/ComputerLock/Shared/MainLayout.razor.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,6 @@ private void InitializeEventBinding()
148148
{
149149
RegisterLockHotkey();
150150
}
151-
if (AppSettings.UnlockHotkeyString.IsNotEmpty())
152-
{
153-
RegisterUnlockHotkey();
154-
}
155151

156152
HotkeyHook.HotkeyPressed += (id) =>
157153
{
@@ -214,21 +210,4 @@ public void RegisterLockHotkey()
214210
Snackbar.Add($"{Lang["ExRegistFailed"]}{ex.Message}", Severity.Error);
215211
}
216212
}
217-
218-
public void RegisterUnlockHotkey()
219-
{
220-
try
221-
{
222-
if (AppSettings.UnlockHotkey != null)
223-
{
224-
Logger.Info("注册解锁热键");
225-
HotkeyHook.Register((int)HotkeyType.Unlock, AppSettings.UnlockHotkey);
226-
}
227-
}
228-
catch (Exception ex)
229-
{
230-
Logger.Error($"绑定解锁热键失败", ex);
231-
Snackbar.Add($"{Lang["ExRegistFailed"]}{ex.Message}", Severity.Error);
232-
}
233-
}
234213
}

0 commit comments

Comments
 (0)