Skip to content

Commit 986284b

Browse files
committed
支持自定义解锁快捷键
1 parent bcd646b commit 986284b

File tree

10 files changed

+174
-31
lines changed

10 files changed

+174
-31
lines changed

src/ComputerLock/Configuration/AppSettings.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,36 @@ public void Initialize(HotkeyTools hotkeyTools)
7373
/// <summary>
7474
/// 锁屏快捷键
7575
/// </summary>
76-
public string ShortcutKeyForLock { get; set; } = "";
76+
[JsonPropertyName("ShortcutKeyForLock")]
77+
public string LockHotkeyString { get; set; } = "";
7778

7879
/// <summary>
7980
/// 锁屏快捷键(映射到按键名称,用于主界面显示)
8081
/// </summary>
8182
[JsonIgnore]
82-
public string LockHotkeyDisplay => _hotkeyTools?.StringKeyToDisplay(ShortcutKeyForLock) ?? "";
83+
public string LockHotkeyDisplay => _hotkeyTools?.StringKeyToDisplay(LockHotkeyString) ?? "";
8384

8485
/// <summary>
8586
/// 锁屏快捷键
8687
/// </summary>
8788
[JsonIgnore]
88-
public Hotkey? LockHotkey => _hotkeyTools?.StringKeyToHotkey(ShortcutKeyForLock);
89+
public Hotkey? LockHotkey => _hotkeyTools?.StringKeyToHotkey(LockHotkeyString);
8990

91+
/// <summary>
92+
/// 解锁时使用锁屏快捷键
93+
/// </summary>
94+
public bool IsUnlockUseLockHotkey { get; set; } = true;
95+
96+
/// <summary>
97+
/// 解锁快捷键
98+
/// </summary>
99+
public string UnlockHotkeyString { get; set; } = "";
100+
101+
/// <summary>
102+
/// 解锁快捷键
103+
/// </summary>
104+
[JsonIgnore]
105+
public Hotkey? UnlockHotkey => _hotkeyTools?.StringKeyToHotkey(UnlockHotkeyString);
90106

91107
/// <summary>
92108
/// 自动检查更新

src/ComputerLock/Enums/HotkeyType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
public enum HotkeyType
44
{
55
Lock = 90,
6-
Unlock = 90,
6+
Unlock = 91,
77
}

src/ComputerLock/Pages/Index.razor

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ else
4646
</MudNumericField>
4747

4848
<HotkeyInput Title="@(Lang["LockHotkey"])"
49-
Hotkey="@(AppSettings.ShortcutKeyForLock)"
50-
OnHotkeySet="SetHotkey"
51-
OnHotkeyClear="ClearHotkey" />
49+
Hotkey="@(AppSettings.LockHotkeyString)"
50+
OnHotkeySet="SetLockHotkey"
51+
OnHotkeyClear="ClearLockHotkey" />
5252

5353
<MudToggleGroup @bind-Value="@(AppSettings.ScreenUnlockMethod)"
5454
@bind-Value:after="SaveSettings"
@@ -131,8 +131,23 @@ else
131131
}
132132
else if (AppSettings.ScreenUnlockMethod == ScreenUnlockMethods.Hotkey)
133133
{
134-
<MudAlert Severity="Severity.Success">当前不支持自定义快捷键,解锁和锁屏快捷键相同</MudAlert>
135134
<MudAlert Severity="Severity.Error">@(Lang["HotkeyUnlockTips"])</MudAlert>
135+
136+
<MudPaper Class="mt-1 d-flex align-center" Elevation="0">
137+
<HotkeyInput Title="@(Lang["UnlockHotkey"])"
138+
Hotkey="@(AppSettings.UnlockHotkeyString)"
139+
Disabled="@AppSettings.IsUnlockUseLockHotkey"
140+
OnHotkeySet="SetUnlockHotkey"
141+
OnHotkeyClear="ClearUnlockHotkey" />
142+
143+
<MudCheckBox @bind-Value="@(AppSettings.IsUnlockUseLockHotkey)"
144+
@bind-Value:after="SaveSettings"
145+
class="ml-3"
146+
Label="@(Lang["UseLockHotkey"])"
147+
Size="Size.Small"
148+
Dense="true"
149+
Color="Color.Primary"></MudCheckBox>
150+
</MudPaper>
136151
}
137152
</MudPaper>
138153
}

src/ComputerLock/Pages/Index.razor.cs

Lines changed: 78 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,40 @@ protected override async Task OnInitializedAsync()
4343
_keyboardDownChecked = (AppSettings.PasswordBoxActiveMethod & PasswordBoxActiveMethodEnum.KeyboardDown) == PasswordBoxActiveMethodEnum.KeyboardDown;
4444
_mouseDownChecked = (AppSettings.PasswordBoxActiveMethod & PasswordBoxActiveMethodEnum.MouseDown) == PasswordBoxActiveMethodEnum.MouseDown;
4545

46-
if (AppSettings.ShortcutKeyForLock.IsNotTrimEmpty())
46+
if (AppSettings.LockHotkeyString.IsNotTrimEmpty())
4747
{
48-
RegisterHotkey();
48+
RegisterLockHotkey();
49+
}
50+
if (AppSettings.UnlockHotkeyString.IsNotTrimEmpty())
51+
{
52+
RegisterUnlockHotkey();
4953
}
5054

5155
HotkeyHook.HotkeyPressed += (id) =>
5256
{
53-
if (GlobalLockService.IsLocked)
57+
if (id == (int)HotkeyType.Lock)
5458
{
55-
Logger.Write("快捷键解锁");
56-
GlobalLockService.Unlock();
59+
if (!GlobalLockService.IsLocked)
60+
{
61+
Logger.Write("快捷键锁定");
62+
GlobalLockService.Lock();
63+
}
64+
else
65+
{
66+
if (AppSettings.ScreenUnlockMethod == ScreenUnlockMethods.Hotkey && AppSettings.IsUnlockUseLockHotkey)
67+
{
68+
Logger.Write("快捷键解锁");
69+
GlobalLockService.Unlock();
70+
}
71+
}
5772
}
58-
else
73+
else if (id == (int)HotkeyType.Unlock)
5974
{
60-
Logger.Write("快捷键锁定");
61-
GlobalLockService.Lock();
75+
if (GlobalLockService.IsLocked)
76+
{
77+
Logger.Write("快捷键解锁");
78+
GlobalLockService.Unlock();
79+
}
6280
}
6381
};
6482

@@ -150,22 +168,37 @@ private void SaveSettings()
150168
{
151169
AppSettingsProvider.SaveSettings(AppSettings);
152170
}
153-
private Task SetHotkey(string hotkey)
171+
private Task SetLockHotkey(string hotkey)
172+
{
173+
AppSettings.LockHotkeyString = hotkey;
174+
SaveSettings();
175+
RegisterLockHotkey();
176+
return Task.CompletedTask;
177+
}
178+
private Task ClearLockHotkey()
179+
{
180+
AppSettings.LockHotkeyString = "";
181+
SaveSettings();
182+
UnregisterLockHotkey();
183+
return Task.CompletedTask;
184+
}
185+
186+
private Task SetUnlockHotkey(string hotkey)
154187
{
155-
AppSettings.ShortcutKeyForLock = hotkey;
188+
AppSettings.UnlockHotkeyString = hotkey;
156189
SaveSettings();
157-
RegisterHotkey();
190+
RegisterUnlockHotkey();
158191
return Task.CompletedTask;
159192
}
160-
private Task ClearHotkey()
193+
private Task ClearUnlockHotkey()
161194
{
162-
AppSettings.ShortcutKeyForLock = "";
195+
AppSettings.UnlockHotkeyString = "";
163196
SaveSettings();
164-
UnregisterHotkey();
197+
UnregisterUnlockHotkey();
165198
return Task.CompletedTask;
166199
}
167200

168-
public void RegisterHotkey()
201+
public void RegisterLockHotkey()
169202
{
170203
try
171204
{
@@ -181,8 +214,7 @@ public void RegisterHotkey()
181214
Snackbar.Add($"{Lang["ExRegistFailed"]}{ex.Message}", Severity.Error);
182215
}
183216
}
184-
185-
public void UnregisterHotkey()
217+
public void UnregisterLockHotkey()
186218
{
187219
try
188220
{
@@ -196,6 +228,35 @@ public void UnregisterHotkey()
196228
}
197229
}
198230

231+
public void RegisterUnlockHotkey()
232+
{
233+
try
234+
{
235+
if (AppSettings.UnlockHotkey != null)
236+
{
237+
Logger.Write("注册解锁热键");
238+
HotkeyHook.Register((int)HotkeyType.Unlock, AppSettings.UnlockHotkey);
239+
}
240+
}
241+
catch (Exception ex)
242+
{
243+
Logger.Write($"绑定解锁热键失败:{ex.Message}{ex.StackTrace}");
244+
Snackbar.Add($"{Lang["ExRegistFailed"]}{ex.Message}", Severity.Error);
245+
}
246+
}
247+
public void UnregisterUnlockHotkey()
248+
{
249+
try
250+
{
251+
Logger.Write("释放解锁热键");
252+
HotkeyHook.Unregister((int)HotkeyType.Unlock);
253+
}
254+
catch (Exception ex)
255+
{
256+
Logger.Write($"释放解锁热键失败:{ex.Message}{ex.StackTrace}");
257+
//MessageBoxUtils.ShowError($"取消快捷键失败:{ex.Message}");
258+
}
259+
}
199260
private async Task ResetPassword()
200261
{
201262
var noHeader = new DialogOptions()

src/ComputerLock/Platforms/HotkeyHook.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,18 @@ public HotkeyHook()
5252
/// </summary>
5353
public void Register(int id, Hotkey hotKey)
5454
{
55+
if (ids.Contains(id))
56+
{
57+
Unregister(id);
58+
}
59+
5560
var success = RegisterHotKey(_nativeWindow.Handle, id, (uint)hotKey.Modifiers, (uint)hotKey.Key);
5661
if (!success)
5762
{
5863
throw new Exception($"注册快捷键失败。id({id})");
5964
}
6065

61-
if (!ids.Contains(id))
62-
{
63-
ids.Add(id);
64-
}
66+
ids.Add(id);
6567
}
6668

6769
/// <summary>

src/ComputerLock/Resources/Lang.Designer.cs

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ComputerLock/Resources/Lang.en.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,4 +360,10 @@
360360
<data name="HotkeyUnlockTips" xml:space="preserve">
361361
<value>If you use this option, please make sure that the hotkey is set, otherwise, it won't unlock! (Appropriate validation may be added in the future).</value>
362362
</data>
363+
<data name="UnlockHotkey" xml:space="preserve">
364+
<value>Unlock Hotkey</value>
365+
</data>
366+
<data name="UseLockHotkey" xml:space="preserve">
367+
<value>Use Lock Hotkey</value>
368+
</data>
363369
</root>

src/ComputerLock/Resources/Lang.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,4 +340,10 @@
340340
<data name="HotkeyUnlockTips" xml:space="preserve">
341341
<value></value>
342342
</data>
343+
<data name="UnlockHotkey" xml:space="preserve">
344+
<value></value>
345+
</data>
346+
<data name="UseLockHotkey" xml:space="preserve">
347+
<value></value>
348+
</data>
343349
</root>

src/ComputerLock/Resources/Lang.zh.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,4 +357,10 @@
357357
<data name="HotkeyUnlockTips" xml:space="preserve">
358358
<value>如果使用此选项,请确保已经设置了快捷键,否则会无法解锁!!(后续可能会加入适当校验)</value>
359359
</data>
360+
<data name="UnlockHotkey" xml:space="preserve">
361+
<value>解锁快捷键</value>
362+
</data>
363+
<data name="UseLockHotkey" xml:space="preserve">
364+
<value>使用锁屏快捷键</value>
365+
</data>
360366
</root>

src/ComputerLock/Services/GlobalLockService.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,23 @@ public void Lock()
104104
_mouseHook.HideCursor();
105105
}
106106

107-
if (_appSettings.ScreenUnlockMethod == ScreenUnlockMethods.Hotkey && _appSettings.LockHotkey != null)
107+
if (_appSettings.ScreenUnlockMethod == ScreenUnlockMethods.Hotkey)
108108
{
109109
_logger.Write("锁定服务 -> 允许快捷键解锁 -> 准备处理热键");
110-
_systemKeyHook.SetIgnoreHotkey(_appSettings.LockHotkey);
110+
if (_appSettings.IsUnlockUseLockHotkey)
111+
{
112+
if (_appSettings.LockHotkey != null)
113+
{
114+
_systemKeyHook.SetIgnoreHotkey(_appSettings.LockHotkey);
115+
}
116+
}
117+
else
118+
{
119+
if (_appSettings.UnlockHotkey != null)
120+
{
121+
_systemKeyHook.SetIgnoreHotkey(_appSettings.UnlockHotkey);
122+
}
123+
}
111124
}
112125
_systemKeyHook.DisableSystemKey();
113126
IsLocked = true;

0 commit comments

Comments
 (0)