diff --git a/src/ChatPrisma/App.xaml.cs b/src/ChatPrisma/App.xaml.cs index c4be754..0e62d05 100644 --- a/src/ChatPrisma/App.xaml.cs +++ b/src/ChatPrisma/App.xaml.cs @@ -121,6 +121,15 @@ private IHostBuilder CreateHostBuilder(string[] args) => Microsoft.Extensions.Ho .Bind(context.Configuration.GetSection("Updater")) .ValidateDataAnnotations() .ValidateOnStart(); + services.AddOptions() + .Configure(o => + { + o.Key = "Y"; + o.KeyModifiers = "Ctrl+Shift+Alt"; + }) + .Bind(context.Configuration.GetSection("Keyboard")) + .ValidateDataAnnotations() + .ValidateOnStart(); // Services services.AddSingleton(); diff --git a/src/ChatPrisma/Options/KeyboardOptions.cs b/src/ChatPrisma/Options/KeyboardOptions.cs new file mode 100644 index 0000000..e6a1a3a --- /dev/null +++ b/src/ChatPrisma/Options/KeyboardOptions.cs @@ -0,0 +1,12 @@ +using System.ComponentModel.DataAnnotations; + +namespace ChatPrisma.Options +{ + public class KeyboardOptions + { + [Required] + public string Key { get; set; } = default!; + [Required] + public string KeyModifiers { get; set; } = default!; + } +} diff --git a/src/ChatPrisma/Services/KeyboardHooks/GlobalKeyInterceptorKeyboardHooks.cs b/src/ChatPrisma/Services/KeyboardHooks/GlobalKeyInterceptorKeyboardHooks.cs index 25a0999..c7292a0 100644 --- a/src/ChatPrisma/Services/KeyboardHooks/GlobalKeyInterceptorKeyboardHooks.cs +++ b/src/ChatPrisma/Services/KeyboardHooks/GlobalKeyInterceptorKeyboardHooks.cs @@ -1,19 +1,23 @@ +using ChatPrisma.Options; using GlobalKeyInterceptor; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using Shortcut = GlobalKeyInterceptor.Shortcut; namespace ChatPrisma.Services.KeyboardHooks; -public class GlobalKeyInterceptorKeyboardHooks(ILogger logger) : IKeyboardHooks, IDisposable +public class GlobalKeyInterceptorKeyboardHooks(ILogger logger, IOptions keyboardOptions) : IKeyboardHooks, IDisposable { public event EventHandler? CombinationPressed; private KeyInterceptor? _interceptor; public Task StartAsync() { + var (key, keyModifiers) = ParseKey(); + var shortcuts = new[] { - new Shortcut(Key.Y, KeyModifier.Ctrl | KeyModifier.Shift | KeyModifier.Alt, "CTRL + SHIFT + ALT + Y"), + new Shortcut(key, keyModifiers, "Prisma Shortcut"), }; this._interceptor = new KeyInterceptor(shortcuts); @@ -37,6 +41,31 @@ private void OnShortcutPressed(object? sender, ShortcutPressedEventArgs e) { logger.LogTrace("Shortcut pressed: {Shortcut}", e.Shortcut.Name); this.CombinationPressed?.Invoke(this, EventArgs.Empty); + + e.IsHandled = true; + } + + private (Key, KeyModifier) ParseKey() + { + try + { + var key = keyboardOptions.Value.Key; + var keyModifiers = keyboardOptions.Value.KeyModifiers; + + var parsedKey = Enum.Parse(key, ignoreCase: true); + + var parsedKeyModifiers = keyModifiers + .Split('+') + .Select(f => f.Trim()) + .Select(f => Enum.Parse(f, ignoreCase: true)) + .Aggregate((x, y) => x | y); + + return (parsedKey, parsedKeyModifiers); + } + catch (Exception) + { + throw new PrismaException("Could not parse keyboard shortcut."); + } } public void Dispose() diff --git a/src/ChatPrisma/appsettings.json b/src/ChatPrisma/appsettings.json index d47a0b6..445f26b 100644 --- a/src/ChatPrisma/appsettings.json +++ b/src/ChatPrisma/appsettings.json @@ -7,6 +7,10 @@ "CheckForUpdatesInBackground": true, "MinutesBetweenUpdateChecks": 10 }, + "Keyboard": { + "Key": "Y", + "KeyModifiers": "Ctrl+Shift+Alt", + }, "NLog": { "throwConfigExceptions": true, "targets": {