mirror of
https://github.com/Stone-Red-Code/InvLock.git
synced 2026-09-04 09:06:13 +02:00
Make shortcuts customizable
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
using System.Reflection;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
|
||||
using SharpHook.Native;
|
||||
|
||||
using System.Reflection;
|
||||
|
||||
namespace InvLock.DataContexts;
|
||||
|
||||
internal class MainWindowDataContext
|
||||
internal partial class MainWindowDataContext(Func<LockWindow?> lockWindowAccessor)
|
||||
{
|
||||
#if DEBUG
|
||||
public string Title => $"{App.AppName} - Dev {AppVersion}";
|
||||
@@ -18,4 +22,40 @@ internal class MainWindowDataContext
|
||||
public string AppVersion => Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "Unknown";
|
||||
|
||||
public Settings Settings { get; } = Settings.Load();
|
||||
|
||||
[RelayCommand]
|
||||
public async Task RecordLockShortcut()
|
||||
{
|
||||
LockWindow? lockWindow = lockWindowAccessor();
|
||||
|
||||
if (lockWindow is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
HashSet<KeyCode>? lockShortcut = await lockWindow.RecordShortcut();
|
||||
|
||||
if (lockShortcut is not null)
|
||||
{
|
||||
Settings.LockShortcut = lockShortcut;
|
||||
}
|
||||
}
|
||||
|
||||
[RelayCommand]
|
||||
public async Task RecordUnlockShortcut()
|
||||
{
|
||||
LockWindow? lockWindow = lockWindowAccessor();
|
||||
|
||||
if (lockWindow is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
HashSet<KeyCode>? unlockShortcut = await lockWindow.RecordShortcut();
|
||||
|
||||
if (unlockShortcut is not null)
|
||||
{
|
||||
Settings.UnlockShortcut = unlockShortcut;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
|
||||
<PackageReference Include="CuteUtils" Version="1.0.0" />
|
||||
<PackageReference Include="SharpHook" Version="5.3.8" />
|
||||
<PackageReference Include="WPF-UI" Version="3.0.5" />
|
||||
|
||||
@@ -17,5 +17,7 @@
|
||||
ShowInTaskbar="False"
|
||||
Title="MainWindow" Height="Auto" Width="Auto">
|
||||
|
||||
<controls:OutlinedTextBlock x:Name="textBlock" Text="Lock Screen Active" Bold="True" Stroke="Red" Opacity="0" Fill="Black" StrokeThickness="1" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="50" Font="Segoe UI" />
|
||||
<Viewbox Stretch="Uniform" StretchDirection="DownOnly">
|
||||
<controls:OutlinedTextBlock x:Name="textBlock" Text="Lock Screen Active" Bold="True" Stroke="Red" Opacity="0" Fill="Black" StrokeThickness="1" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="50" Font="Segoe UI" />
|
||||
</Viewbox>
|
||||
</Window>
|
||||
@@ -3,6 +3,7 @@
|
||||
using SharpHook;
|
||||
using SharpHook.Native;
|
||||
|
||||
using System.Diagnostics;
|
||||
using System.Windows;
|
||||
using System.Windows.Interop;
|
||||
using System.Windows.Media;
|
||||
@@ -59,6 +60,59 @@ public partial class LockWindow : Window
|
||||
_ = Task.Run(hook.Run);
|
||||
}
|
||||
|
||||
public async Task<HashSet<KeyCode>?> RecordShortcut()
|
||||
{
|
||||
HashSet<KeyCode> keys = [];
|
||||
|
||||
hook.KeyPressed -= Hook_KeyPressed;
|
||||
hook.KeyPressed += Record;
|
||||
|
||||
textBlock.Text = "Press a key combination to record a shortcut";
|
||||
textBlock.Stroke = Brushes.White;
|
||||
textBlock.Opacity = 1;
|
||||
|
||||
// Wait for the user to press the enter key
|
||||
await Task.Run(async () =>
|
||||
{
|
||||
while (!keys.Contains(KeyCode.VcEnter) && !keys.Contains(KeyCode.VcEscape))
|
||||
{
|
||||
await Task.Delay(100);
|
||||
}
|
||||
});
|
||||
|
||||
hook.KeyPressed -= Record;
|
||||
hook.KeyPressed += Hook_KeyPressed;
|
||||
|
||||
textBlock.Opacity = 0;
|
||||
|
||||
if (keys.Contains(KeyCode.VcEscape) || keys.Count <= 1)
|
||||
{
|
||||
textBlock.Text = "Shortcut Recording Cancelled";
|
||||
textBlock.Stroke = (Brush)FindResource("PaletteRedBrush");
|
||||
Animation();
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
textBlock.Text = "Shortcut Recorded";
|
||||
textBlock.Stroke = (Brush)FindResource("PaletteGreenBrush");
|
||||
Animation();
|
||||
|
||||
_ = keys.Remove(KeyCode.VcEnter);
|
||||
|
||||
return keys;
|
||||
|
||||
void Record(object? sender, KeyboardHookEventArgs e)
|
||||
{
|
||||
e.SuppressEvent = true;
|
||||
_ = keys.Add(e.Data.KeyCode);
|
||||
Debug.WriteLine(string.Join(" + ", keys.Select(key => key.ToString())));
|
||||
_ = Dispatcher.Invoke(() => textBlock.Text = string.Join(" + ", keys.Select(key => key.ToString()[2..]))
|
||||
+ Environment.NewLine
|
||||
+ "Press ENTER to save or ESCAPE to cancel");
|
||||
}
|
||||
}
|
||||
|
||||
private void ActivateLockScreen()
|
||||
{
|
||||
if (settings.HideWindows)
|
||||
@@ -82,8 +136,30 @@ public partial class LockWindow : Window
|
||||
|
||||
private void DeactivateLockScreen()
|
||||
{
|
||||
isOpen = false;
|
||||
Dispatcher.Invoke(Close);
|
||||
if (settings.UseWindowsLockScreen)
|
||||
{
|
||||
isOpen = false;
|
||||
|
||||
Dispatcher.Invoke(Close);
|
||||
}
|
||||
else
|
||||
{
|
||||
Dispatcher.Invoke(() =>
|
||||
{
|
||||
if (settings.HideWindows)
|
||||
{
|
||||
RestoreWindows();
|
||||
}
|
||||
|
||||
textBlock.Text = settings.UnlockText;
|
||||
textBlock.Stroke = (Brush)FindResource("PaletteGreenBrush");
|
||||
|
||||
Animation();
|
||||
|
||||
isOpen = true;
|
||||
suppressInput = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void Hook_KeyPressed(object? sender, KeyboardHookEventArgs e)
|
||||
@@ -97,16 +173,13 @@ public partial class LockWindow : Window
|
||||
}
|
||||
_ = pressedKeys[e.Data.KeyCode] = DateTime.UtcNow;
|
||||
|
||||
if (pressedKeys.ContainsKey(KeyCode.VcL) && pressedKeys.ContainsKey(KeyCode.VcLeftShift) && pressedKeys.ContainsKey(KeyCode.VcLeftControl) && pressedKeys.Count == 3)
|
||||
if (pressedKeys.All(kvp => settings.UnlockShortcut.Contains(kvp.Key)) && suppressInput && pressedKeys.Count == settings.UnlockShortcut.Count)
|
||||
{
|
||||
if (suppressInput)
|
||||
{
|
||||
DeactivateLockScreen();
|
||||
}
|
||||
else
|
||||
{
|
||||
ActivateLockScreen();
|
||||
}
|
||||
DeactivateLockScreen();
|
||||
}
|
||||
else if (pressedKeys.All(kvp => settings.LockShortcut.Contains(kvp.Key)) && !suppressInput && pressedKeys.Count == settings.LockShortcut.Count)
|
||||
{
|
||||
ActivateLockScreen();
|
||||
}
|
||||
|
||||
e.SuppressEvent = suppressInput;
|
||||
@@ -171,7 +244,11 @@ public partial class LockWindow : Window
|
||||
|
||||
private void Animation()
|
||||
{
|
||||
Storyboard storyboard = new Storyboard();
|
||||
Storyboard storyboard = new Storyboard
|
||||
{
|
||||
FillBehavior = FillBehavior.Stop
|
||||
};
|
||||
|
||||
TimeSpan duration = TimeSpan.FromMilliseconds(500);
|
||||
|
||||
DoubleAnimation fadeInAnimation = new DoubleAnimation()
|
||||
|
||||
@@ -56,10 +56,6 @@
|
||||
It is designed to be lightweight and easy to use.
|
||||
<LineBreak />
|
||||
If you have any issues or feature requests, please report them on GitHub.
|
||||
<LineBreak />
|
||||
<LineBreak />
|
||||
To lock and unlock your screen, press
|
||||
<ui:TextBlock Foreground="{DynamicResource AccentTextFillColorPrimaryBrush}">WIN + SHIFT + L</ui:TextBlock>
|
||||
</ui:TextBlock>
|
||||
|
||||
<ui:TextBlock Margin="0,0,0,8" FontTypography="BodyStrong" Text="Appearance & behavior" />
|
||||
@@ -90,6 +86,13 @@
|
||||
<ui:ToggleSwitch Grid.Column="1" IsChecked="{Binding Settings.HideWindows}" OffContent="Off" OnContent="On" />
|
||||
</ui:CardControl>
|
||||
|
||||
<ui:CardControl Margin="0,0,0,12" Icon="{ui:SymbolIcon Symbol=WindowShield24}" Height="75">
|
||||
<ui:CardControl.Header>
|
||||
<ui:TextBlock Grid.Row="0" FontTypography="Body" Text="Use windows lock screen to unlock" />
|
||||
</ui:CardControl.Header>
|
||||
<ui:ToggleSwitch Grid.Column="1" IsChecked="{Binding Settings.UseWindowsLockScreen}" OffContent="Off" OnContent="On" />
|
||||
</ui:CardControl>
|
||||
|
||||
<ui:CardControl Margin="0,0,0,12" Icon="{ui:SymbolIcon Symbol=LockClosed24}" Height="75">
|
||||
<ui:CardControl.Header>
|
||||
<ui:TextBlock Grid.Row="0" FontTypography="Body" Text="Lock text" />
|
||||
@@ -97,6 +100,13 @@
|
||||
<ui:TextBox Grid.Column="1" MinWidth="200" Text="{Binding Settings.LockText, UpdateSourceTrigger=PropertyChanged}" PlaceholderText="Type your lock text here" />
|
||||
</ui:CardControl>
|
||||
|
||||
<ui:CardControl Margin="0,0,0,12" Icon="{ui:SymbolIcon Symbol=LockClosed24}" Height="75">
|
||||
<ui:CardControl.Header>
|
||||
<ui:TextBlock Grid.Row="0" FontTypography="Body" Text="Lock shortcut" />
|
||||
</ui:CardControl.Header>
|
||||
<ui:Button Grid.Column="1" Command="{Binding RecordLockShortcutCommand}" MinWidth="200" Icon="{ui:SymbolIcon Symbol=Edit24}" Content="{Binding Settings.LockShortcutText}" />
|
||||
</ui:CardControl>
|
||||
|
||||
<ui:CardControl Margin="0,0,0,12" Icon="{ui:SymbolIcon Symbol=LockOpen24}" Height="75">
|
||||
<ui:CardControl.Header>
|
||||
<ui:TextBlock Grid.Row="0" FontTypography="Body" Text="Unlock text" />
|
||||
@@ -104,6 +114,13 @@
|
||||
<ui:TextBox Grid.Column="1" MinWidth="200" Text="{Binding Settings.UnlockText, UpdateSourceTrigger=PropertyChanged}" PlaceholderText="Type your unlock text here" />
|
||||
</ui:CardControl>
|
||||
|
||||
<ui:CardControl Margin="0,0,0,12" Icon="{ui:SymbolIcon Symbol=LockOpen24}" Height="75">
|
||||
<ui:CardControl.Header>
|
||||
<ui:TextBlock Grid.Row="0" FontTypography="Body" Text="Unlock shortcut" />
|
||||
</ui:CardControl.Header>
|
||||
<ui:Button Grid.Column="1" Command="{Binding RecordUnlockShortcutCommand}" MinWidth="200" Icon="{ui:SymbolIcon Symbol=Edit24}" Content="{Binding Settings.UnlockShortcutText}" />
|
||||
</ui:CardControl>
|
||||
|
||||
<ui:TextBlock Margin="0,24,0,8" FontTypography="BodyStrong" Text="About" />
|
||||
<ui:CardExpander ContentPadding="0">
|
||||
<ui:CardExpander.Header>
|
||||
@@ -131,7 +148,7 @@
|
||||
<TextBlock Grid.Column="0" Text="Report an issue or request a feature" />
|
||||
<StackPanel Grid.Column="1" Orientation="Horizontal">
|
||||
<ui:SymbolIcon Margin="0 0 6 0" Symbol="Link24" />
|
||||
<TextBlock Foreground="{ui:ThemeResource TextFillColorSecondaryBrush}" Text="https://github.com/Stone-Red-Code/InvLock/issues"/>
|
||||
<TextBlock Foreground="{ui:ThemeResource TextFillColorSecondaryBrush}" Text="https://github.com/Stone-Red-Code/InvLock/issues" />
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</ui:Anchor>
|
||||
@@ -147,7 +164,7 @@
|
||||
<TextBlock Grid.Column="0" Text="GitHub" />
|
||||
<StackPanel Grid.Column="1" Orientation="Horizontal">
|
||||
<ui:SymbolIcon Margin="0 0 6 0" Symbol="Link24" />
|
||||
<TextBlock Foreground="{ui:ThemeResource TextFillColorSecondaryBrush}" Text="https://github.com/Stone-Red-Code/InvLock"/>
|
||||
<TextBlock Foreground="{ui:ThemeResource TextFillColorSecondaryBrush}" Text="https://github.com/Stone-Red-Code/InvLock" />
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</ui:Anchor>
|
||||
@@ -160,7 +177,7 @@
|
||||
<ColumnDefinition Width="Auto" />
|
||||
</Grid.ColumnDefinitions>
|
||||
<TextBlock Grid.Column="0" Text="Version" />
|
||||
<TextBlock Grid.Column="1" Foreground="{ui:ThemeResource TextFillColorSecondaryBrush}" Text="{Binding AppVersion}"/>
|
||||
<TextBlock Grid.Column="1" Foreground="{ui:ThemeResource TextFillColorSecondaryBrush}" Text="{Binding AppVersion}" />
|
||||
</Grid>
|
||||
</StackPanel>
|
||||
</ui:CardExpander>
|
||||
|
||||
@@ -12,12 +12,13 @@ namespace InvLock;
|
||||
/// </summary>
|
||||
public partial class MainWindow : FluentWindow
|
||||
{
|
||||
private readonly MainWindowDataContext mainWindowDataContext = new MainWindowDataContext();
|
||||
private readonly MainWindowDataContext mainWindowDataContext;
|
||||
private LockWindow? lockWindow;
|
||||
private bool blockWindowClosing = true;
|
||||
|
||||
public MainWindow()
|
||||
{
|
||||
mainWindowDataContext = new MainWindowDataContext(() => lockWindow);
|
||||
DataContext = mainWindowDataContext;
|
||||
|
||||
InitializeComponent();
|
||||
|
||||
+54
-4
@@ -1,7 +1,10 @@
|
||||
using System.ComponentModel;
|
||||
using SharpHook.Native;
|
||||
|
||||
using System.ComponentModel;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
using Wpf.Ui.Appearance;
|
||||
|
||||
@@ -12,9 +15,12 @@ public class Settings : INotifyPropertyChanged
|
||||
public event PropertyChangedEventHandler? PropertyChanged;
|
||||
|
||||
private static readonly string settingsPath = Path.Combine(App.ApplicationDataPath, "settings.json");
|
||||
private bool useWindowsLockScreen = true;
|
||||
private bool hideWindows = false;
|
||||
private string lockText = "Lock Screen Active";
|
||||
private string theme = "Windows default";
|
||||
private HashSet<KeyCode> lockShortcut = [KeyCode.VcLeftControl, KeyCode.VcLeftShift, KeyCode.VcL];
|
||||
private HashSet<KeyCode> unlockShortcut = [KeyCode.VcLeftControl, KeyCode.VcLeftShift, KeyCode.VcL];
|
||||
|
||||
private string unlockText = "Lock Screen Inactive";
|
||||
|
||||
@@ -28,6 +34,16 @@ public class Settings : INotifyPropertyChanged
|
||||
}
|
||||
}
|
||||
|
||||
public bool UseWindowsLockScreen
|
||||
{
|
||||
get => useWindowsLockScreen;
|
||||
set
|
||||
{
|
||||
useWindowsLockScreen = value;
|
||||
OnPropertyChanged();
|
||||
}
|
||||
}
|
||||
|
||||
public string LockText
|
||||
{
|
||||
get => lockText;
|
||||
@@ -38,6 +54,20 @@ public class Settings : INotifyPropertyChanged
|
||||
}
|
||||
}
|
||||
|
||||
public HashSet<KeyCode> LockShortcut
|
||||
{
|
||||
get => lockShortcut;
|
||||
set
|
||||
{
|
||||
lockShortcut = value;
|
||||
OnPropertyChanged();
|
||||
OnPropertyChanged(nameof(LockShortcutText));
|
||||
}
|
||||
}
|
||||
|
||||
[JsonIgnore]
|
||||
public string LockShortcutText => string.Join(" + ", LockShortcut.Select(key => key.ToString()[2..]));
|
||||
|
||||
public string UnlockText
|
||||
{
|
||||
get => unlockText;
|
||||
@@ -48,6 +78,20 @@ public class Settings : INotifyPropertyChanged
|
||||
}
|
||||
}
|
||||
|
||||
public HashSet<KeyCode> UnlockShortcut
|
||||
{
|
||||
get => unlockShortcut;
|
||||
set
|
||||
{
|
||||
unlockShortcut = value;
|
||||
OnPropertyChanged();
|
||||
OnPropertyChanged(nameof(UnlockShortcutText));
|
||||
}
|
||||
}
|
||||
|
||||
[JsonIgnore]
|
||||
public string UnlockShortcutText => string.Join(" + ", UnlockShortcut.Select(key => key.ToString()[2..]));
|
||||
|
||||
public string Theme
|
||||
{
|
||||
get => theme;
|
||||
@@ -76,10 +120,16 @@ public class Settings : INotifyPropertyChanged
|
||||
{
|
||||
if (File.Exists(settingsPath))
|
||||
{
|
||||
string json = File.ReadAllText(settingsPath);
|
||||
return JsonSerializer.Deserialize<Settings>(json) ?? new Settings();
|
||||
try
|
||||
{
|
||||
string json = File.ReadAllText(settingsPath);
|
||||
return JsonSerializer.Deserialize<Settings>(json) ?? new Settings();
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
return new Settings();
|
||||
}
|
||||
}
|
||||
|
||||
return new Settings();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user