Skip to content

Commit d154ea4

Browse files
committed
支持多个快捷键的注册
1 parent c8fbe60 commit d154ea4

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace ComputerLock.Enums;
2+
3+
public enum HotkeyType
4+
{
5+
Lock = 90,
6+
Unlock = 90,
7+
}

src/ComputerLock/Pages/Index.razor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected override async Task OnInitializedAsync()
4848
RegisterHotkey();
4949
}
5050

51-
HotkeyHook.HotkeyPressed += () =>
51+
HotkeyHook.HotkeyPressed += (id) =>
5252
{
5353
if (GlobalLockService.IsLocked)
5454
{
@@ -172,7 +172,7 @@ public void RegisterHotkey()
172172
if (AppSettings.LockHotkey != null)
173173
{
174174
Logger.Write("注册锁屏热键");
175-
HotkeyHook.Register(AppSettings.LockHotkey);
175+
HotkeyHook.Register((int)HotkeyType.Lock, AppSettings.LockHotkey);
176176
}
177177
}
178178
catch (Exception ex)
@@ -187,7 +187,7 @@ public void UnregisterHotkey()
187187
try
188188
{
189189
Logger.Write("释放锁屏热键");
190-
HotkeyHook.Unregister();
190+
HotkeyHook.Unregister((int)HotkeyType.Lock);
191191
}
192192
catch (Exception ex)
193193
{

src/ComputerLock/Platforms/HotkeyHook.cs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,19 @@ namespace ComputerLock.Platforms;
77
/// </summary>
88
public class HotkeyHook : IDisposable
99
{
10+
private List<int> ids = new List<int>();
11+
1012
[DllImport("user32.dll")]
1113
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
1214

1315
[DllImport("user32.dll")]
1416
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
1517

16-
private const int HotkeyId = 90;
17-
private bool _isRegistered;
18-
19-
public event Action? HotkeyPressed;
18+
public event Action<int>? HotkeyPressed;
2019

2120
private sealed class HotkeyNativeWindow : NativeWindow
2221
{
23-
public event Action? OnHotkeyPressed;
22+
public event Action<int>? OnHotkeyPressed;
2423

2524
public HotkeyNativeWindow()
2625
{
@@ -31,10 +30,7 @@ protected override void WndProc(ref Message m)
3130
{
3231
if (m.Msg == 0x0312) // WM_HOTKEY
3332
{
34-
if (m.WParam.ToInt32() == HotkeyId)
35-
{
36-
OnHotkeyPressed?.Invoke();
37-
}
33+
OnHotkeyPressed?.Invoke(m.WParam.ToInt32());
3834
}
3935
else
4036
{
@@ -48,38 +44,32 @@ protected override void WndProc(ref Message m)
4844
public HotkeyHook()
4945
{
5046
_nativeWindow = new HotkeyNativeWindow();
51-
_nativeWindow.OnHotkeyPressed += () => HotkeyPressed?.Invoke();
47+
_nativeWindow.OnHotkeyPressed += (id) => HotkeyPressed?.Invoke(id);
5248
}
5349

5450
/// <summary>
5551
/// 注册快捷键
5652
/// </summary>
57-
public void Register(Hotkey hotKey)
53+
public void Register(int id, Hotkey hotKey)
5854
{
59-
if (_isRegistered)
55+
var success = RegisterHotKey(_nativeWindow.Handle, id, (uint)hotKey.Modifiers, (uint)hotKey.Key);
56+
if (!success)
6057
{
61-
Unregister();
58+
throw new Exception($"注册快捷键失败。id({id})");
6259
}
6360

64-
var success = RegisterHotKey(_nativeWindow.Handle, HotkeyId, (uint)hotKey.Modifiers, (uint)hotKey.Key);
65-
if (!success)
61+
if (!ids.Contains(id))
6662
{
67-
throw new Exception("注册快捷键失败");
63+
ids.Add(id);
6864
}
69-
_isRegistered = success;
7065
}
7166

7267
/// <summary>
7368
/// 注销快捷键
7469
/// </summary>
75-
public void Unregister()
70+
public void Unregister(int id)
7671
{
77-
if (!_isRegistered)
78-
{
79-
return;
80-
}
81-
UnregisterHotKey(_nativeWindow.Handle, HotkeyId);
82-
_isRegistered = false;
72+
UnregisterHotKey(_nativeWindow.Handle, id);
8373
}
8474

8575
public void Dispose()
@@ -95,7 +85,10 @@ protected virtual void Dispose(bool disposing)
9585
return;
9686
}
9787

98-
Unregister();
88+
foreach (var id in ids)
89+
{
90+
Unregister(id);
91+
}
9992
_nativeWindow.DestroyHandle();
10093
}
10194

0 commit comments

Comments
 (0)