Skip to content

Commit a7a0606

Browse files
committed
支持快捷键锁屏
1 parent 05ef5e1 commit a7a0606

File tree

14 files changed

+448
-177
lines changed

14 files changed

+448
-177
lines changed

src/ComputerLock/App.xaml.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,14 @@ private void Init()
5050
services.AddSingleton<WindowMain>();
5151
services.AddTransient<WindowLockScreen>();
5252
services.AddTransient<WindowBlankScreen>();
53-
services.AddSingleton<ScreenLockService>();
5453
services.AddSingleton<SystemKeyHook>();
54+
services.AddSingleton<GlobalSettings>();
5555
services.AddSingleton<IWindowMoving, WindowMoving>();
5656
services.AddSingleton<IWindowTitleBar, WindowTitleBar>();
5757
services.AddSingleton<IGlobalLockService, GlobalLockService>();
58+
services.AddKeyedSingleton<IScreenLockService, PasswordScreenLockService>(ScreenUnlockMethods.Password);
59+
services.AddKeyedSingleton<IScreenLockService, HotkeyScreenLockService>(ScreenUnlockMethods.Hotkey);
60+
5861
services.AddLocalization();
5962
services.AddWpfBlazorWebView();
6063
services.AddBlazorWebViewDeveloperTools();

src/ComputerLock/Configuration/AppSettings.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,9 @@ public class AppSettings
9393
/// 自动隐藏鼠标光标
9494
/// </summary>
9595
public bool IsHideMouseCursor { get; set; } = false;
96+
97+
/// <summary>
98+
/// 屏幕解锁方式
99+
/// </summary>
100+
public ScreenUnlockMethods ScreenUnlockMethod { get; set; } = ScreenUnlockMethods.Password;
96101
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace ComputerLock.Configuration;
2+
3+
internal class GlobalSettings
4+
{
5+
public HotKey? HotKey { get; set; }
6+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.ComponentModel;
2+
3+
namespace ComputerLock.Enums;
4+
5+
/// <summary>
6+
/// 屏幕解锁方法
7+
/// </summary>
8+
public enum ScreenUnlockMethods
9+
{
10+
[Description("快捷键解锁")]
11+
Hotkey = 1,
12+
[Description("密码解锁")]
13+
Password = 2
14+
}

src/ComputerLock/Interfaces/IGlobalLockService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@
55
/// </summary>
66
public interface IGlobalLockService : IDisposable
77
{
8+
bool IsLocked { get; }
89
void Lock();
10+
void Unlock();
911
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace ComputerLock.Interfaces;
2+
3+
/// <summary>
4+
/// 屏幕锁定接口,负责屏幕窗口的锁定和解锁
5+
/// </summary>
6+
internal interface IScreenLockService
7+
{
8+
event EventHandler? OnUnlock;
9+
bool Lock(bool showAnimation);
10+
void Unlock();
11+
}

src/ComputerLock/Pages/Index.razor

Lines changed: 101 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -31,61 +31,6 @@ else
3131
Label="@(Lang["HideMouseCursor"])"
3232
Color="Color.Primary" />
3333

34-
<div class="d-flex align-center">
35-
<MudSwitch @bind-Value="@(AppSettings.EnablePasswordBox)"
36-
@bind-Value:after="SaveSettings"
37-
Label="@(Lang["EnablePasswordBox"])"
38-
Color="Color.Primary" />
39-
@if (!AppSettings.EnablePasswordBox)
40-
{
41-
<MudChip T="string"
42-
Label="true"
43-
Icon="@Icons.Material.Filled.WarningAmber"
44-
IconColor="Color.Primary"
45-
DisableRipple="true"
46-
Size="Size.Small">
47-
@(Lang["EnablePasswordBoxTips"])
48-
</MudChip>
49-
}
50-
</div>
51-
52-
<MudSwitch @bind-Value="@(AppSettings.IsHidePasswordWindow)"
53-
@bind-Value:after="SaveSettings"
54-
Class="ml-6"
55-
Disabled="@(!AppSettings.EnablePasswordBox)"
56-
Label="@(Lang["HidePasswordWindow"])"
57-
Color="Color.Primary" />
58-
59-
<MudSwitch @bind-Value="@(_keyboardDownChecked)"
60-
@bind-Value:after="KeyboardDownChecked"
61-
Class="ml-6"
62-
Disabled="@(!AppSettings.EnablePasswordBox)"
63-
Label="@(Lang["KeyboardDownActivePwd"])"
64-
Color="Color.Primary" />
65-
66-
<MudSwitch @bind-Value="@(_mouseDownChecked)"
67-
@bind-Value:after="MouseDownChecked"
68-
Class="ml-6"
69-
Disabled="@(!AppSettings.EnablePasswordBox)"
70-
Label="@(Lang["MouseDownActivePwd"])"
71-
Color="Color.Primary" />
72-
73-
<MudSelect T="ScreenLocationEnum"
74-
Label="@(Lang["PwdLocation"])"
75-
Variant="Variant.Outlined"
76-
Value="@(AppSettings.PasswordInputLocation)"
77-
Disabled="@(!AppSettings.EnablePasswordBox)"
78-
Class="ml-6"
79-
ValueChanged="PwdBoxLocationChanged"
80-
Margin="Margin.Dense"
81-
Dense="true">
82-
<MudSelectItem Value="@(ScreenLocationEnum.Center)">@(Lang["Center"])</MudSelectItem>
83-
<MudSelectItem Value="@(ScreenLocationEnum.TopLeft)">@(Lang["TopLeft"])</MudSelectItem>
84-
<MudSelectItem Value="@(ScreenLocationEnum.TopRight)">@(Lang["TopRight"])</MudSelectItem>
85-
<MudSelectItem Value="@(ScreenLocationEnum.BottomLeft)">@(Lang["BottomLeft"])</MudSelectItem>
86-
<MudSelectItem Value="@(ScreenLocationEnum.BottomRight)">@(Lang["BottomRight"])</MudSelectItem>
87-
</MudSelect>
88-
8934
<MudSwitch @bind-Value="@(AppSettings.LockAnimation)"
9035
@bind-Value:after="SaveSettings"
9136
Label="@(Lang["LockAnimation"])"
@@ -96,35 +41,111 @@ else
9641
Value="@(AppSettings.AutoLockMinute)"
9742
Margin="Margin.Dense"
9843
Min="0"
99-
Class="mt-2"
10044
Variant="Variant.Outlined"
10145
ValueChanged="AutoLockChanged">
10246
</MudNumericField>
10347

104-
<MudPaper Class="d-flex justify-space-between mt-4" Elevation="0">
105-
<MudPaper Class="d-flex align-center" Elevation="0">
106-
<MudText Typo="Typo.button">@(Lang["LockShortcutKey"])</MudText>
107-
<MudButton Class="ml-3"
108-
Size="Size.Small"
109-
Color="Color.Primary"
110-
OnClick="SetShortcutKey">
111-
@(_shortcutKeyText)
112-
</MudButton>
113-
@if (AppSettings.ShortcutKeyForLock.IsNotEmpty())
114-
{
115-
<MudTooltip Text="@(Lang["DeleteShortcutKeys"])" Delay="600">
116-
<MudIconButton Icon="@Icons.Material.Filled.Delete"
117-
Size="Size.Small"
118-
OnClick="ClearShortcutKey" />
119-
</MudTooltip>
120-
}
121-
</MudPaper>
122-
123-
<MudButton Color="Color.Primary"
124-
StartIcon="@Icons.Material.Filled.VpnKey"
125-
IconSize="Size.Small"
126-
OnClick="ResetPassword">
127-
@(Lang["ResetPassword"])
48+
<MudPaper Class="d-flex align-center" Elevation="0">
49+
<MudText Typo="Typo.button">@(Lang["LockShortcutKey"])</MudText>
50+
<MudButton Class="ml-3"
51+
Size="Size.Small"
52+
Color="Color.Primary"
53+
OnClick="SetShortcutKey">
54+
@(_shortcutKeyText)
12855
</MudButton>
56+
@if (AppSettings.ShortcutKeyForLock.IsNotEmpty())
57+
{
58+
<MudTooltip Text="@(Lang["DeleteShortcutKeys"])" Delay="600">
59+
<MudIconButton Icon="@Icons.Material.Filled.Delete"
60+
Size="Size.Small"
61+
OnClick="ClearShortcutKey" />
62+
</MudTooltip>
63+
}
64+
</MudPaper>
65+
66+
<MudToggleGroup @bind-Value="@(AppSettings.ScreenUnlockMethod)"
67+
@bind-Value:after="SaveSettings"
68+
T="ScreenUnlockMethods"
69+
SelectionMode="SelectionMode.SingleSelection"
70+
Class="mt-2"
71+
Color="Color.Primary"
72+
Size="Size.Small"
73+
CheckMark
74+
FixedContent>
75+
<MudToggleItem Value="@(ScreenUnlockMethods.Password)" Text="@(ScreenUnlockMethods.Password.GetDescription())" />
76+
<MudToggleItem Value="@(ScreenUnlockMethods.Hotkey)" Text="@(ScreenUnlockMethods.Hotkey.GetDescription())" />
77+
</MudToggleGroup>
78+
79+
<MudPaper Elevation="0" Height="210px">
80+
81+
@if (AppSettings.ScreenUnlockMethod == ScreenUnlockMethods.Password)
82+
{
83+
<div class="d-flex align-center">
84+
<MudSwitch @bind-Value="@(AppSettings.EnablePasswordBox)"
85+
@bind-Value:after="SaveSettings"
86+
Label="@(Lang["EnablePasswordBox"])"
87+
Color="Color.Primary" />
88+
@if (!AppSettings.EnablePasswordBox)
89+
{
90+
<MudChip T="string"
91+
Label="true"
92+
Icon="@Icons.Material.Filled.WarningAmber"
93+
IconColor="Color.Primary"
94+
DisableRipple="true"
95+
Size="Size.Small">
96+
@(Lang["EnablePasswordBoxTips"])
97+
</MudChip>
98+
}
99+
</div>
100+
101+
<MudSwitch @bind-Value="@(AppSettings.IsHidePasswordWindow)"
102+
@bind-Value:after="SaveSettings"
103+
Class="ml-6"
104+
Disabled="@(!AppSettings.EnablePasswordBox)"
105+
Label="@(Lang["HidePasswordWindow"])"
106+
Color="Color.Primary" />
107+
108+
<MudSwitch @bind-Value="@(_keyboardDownChecked)"
109+
@bind-Value:after="KeyboardDownChecked"
110+
Class="ml-6"
111+
Disabled="@(!AppSettings.EnablePasswordBox)"
112+
Label="@(Lang["KeyboardDownActivePwd"])"
113+
Color="Color.Primary" />
114+
115+
<MudSwitch @bind-Value="@(_mouseDownChecked)"
116+
@bind-Value:after="MouseDownChecked"
117+
Class="ml-6"
118+
Disabled="@(!AppSettings.EnablePasswordBox)"
119+
Label="@(Lang["MouseDownActivePwd"])"
120+
Color="Color.Primary" />
121+
122+
<MudSelect T="ScreenLocationEnum"
123+
Label="@(Lang["PwdLocation"])"
124+
Variant="Variant.Outlined"
125+
Value="@(AppSettings.PasswordInputLocation)"
126+
Disabled="@(!AppSettings.EnablePasswordBox)"
127+
Class="ml-6"
128+
ValueChanged="PwdBoxLocationChanged"
129+
Margin="Margin.Dense"
130+
Dense="true">
131+
<MudSelectItem Value="@(ScreenLocationEnum.Center)">@(Lang["Center"])</MudSelectItem>
132+
<MudSelectItem Value="@(ScreenLocationEnum.TopLeft)">@(Lang["TopLeft"])</MudSelectItem>
133+
<MudSelectItem Value="@(ScreenLocationEnum.TopRight)">@(Lang["TopRight"])</MudSelectItem>
134+
<MudSelectItem Value="@(ScreenLocationEnum.BottomLeft)">@(Lang["BottomLeft"])</MudSelectItem>
135+
<MudSelectItem Value="@(ScreenLocationEnum.BottomRight)">@(Lang["BottomRight"])</MudSelectItem>
136+
</MudSelect>
137+
138+
<MudButton Color="Color.Primary"
139+
StartIcon="@Icons.Material.Filled.VpnKey"
140+
IconSize="Size.Small"
141+
OnClick="ResetPassword">
142+
@(Lang["ResetPassword"])
143+
</MudButton>
144+
}
145+
else if (AppSettings.ScreenUnlockMethod == ScreenUnlockMethods.Hotkey)
146+
{
147+
<MudAlert Severity="Severity.Success">当前不支持自定义快捷键,解锁和锁屏快捷键相同</MudAlert>
148+
<MudAlert Severity="Severity.Error">如果使用此选项,请确保已经设置了快捷键,否则会无法解锁!!(后续可能会加入适当校验)</MudAlert>
149+
}
129150
</MudPaper>
130151
}

src/ComputerLock/Pages/Index.razor.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public partial class Index
3434
[Inject]
3535
private ILogger Logger { get; set; } = default!;
3636

37+
[Inject]
38+
private GlobalSettings GlobalSettings { get; set; } = default!;
39+
40+
3741
private bool _keyboardDownChecked;
3842
private bool _mouseDownChecked;
3943
private string _shortcutKeyText = "未设置";
@@ -50,8 +54,16 @@ protected override async Task OnInitializedAsync()
5054

5155
HotKeyHook.HotKeyPressed += () =>
5256
{
53-
Logger.Write("快捷键解锁");
54-
GlobalLockService.Lock();
57+
if (GlobalLockService.IsLocked)
58+
{
59+
Logger.Write("快捷键解锁");
60+
GlobalLockService.Unlock();
61+
}
62+
else
63+
{
64+
Logger.Write("快捷键锁定");
65+
GlobalLockService.Lock();
66+
}
5567
};
5668

5769
if (AppSettings.IsAutoCheckUpdate)
@@ -162,11 +174,6 @@ private async Task SetShortcutKey()
162174
return;
163175
}
164176

165-
// 有历史快捷键时先解除绑定
166-
if (AppSettings.ShortcutKeyForLock.IsNotTrimEmpty())
167-
{
168-
UnregisterHotKey();
169-
}
170177
AppSettings.ShortcutKeyForLock = shortcutKey.Key;
171178
AppSettings.ShortcutKeyDisplayForLock = shortcutKey.DisplayText;
172179
SaveSettings();
@@ -178,6 +185,7 @@ private Task ClearShortcutKey()
178185
AppSettings.ShortcutKeyDisplayForLock = "";
179186
SaveSettings();
180187
UnregisterHotKey();
188+
GlobalSettings.HotKey = null;
181189
return Task.CompletedTask;
182190
}
183191

@@ -206,7 +214,8 @@ public void RegisterHotKey()
206214
}
207215
Logger.Write("注册锁屏热键");
208216
Keys key = (Keys)Convert.ToInt32(result.result);
209-
HotKeyHook.Register(new HotKey(modifiers, key));
217+
GlobalSettings.HotKey = new HotKey(modifiers, key);
218+
HotKeyHook.Register(GlobalSettings.HotKey);
210219

211220
_shortcutKeyText = AppSettings.ShortcutKeyDisplayForLock;
212221
}

0 commit comments

Comments
 (0)