Skip to content

Commit

Permalink
Make keyboard shortcut configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
haefele committed Oct 16, 2023
1 parent 77dfb1a commit 5583e5f
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/ChatPrisma/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,15 @@ private IHostBuilder CreateHostBuilder(string[] args) => Microsoft.Extensions.Ho
.Bind(context.Configuration.GetSection("Updater"))
.ValidateDataAnnotations()
.ValidateOnStart();
services.AddOptions<KeyboardOptions>()
.Configure(o =>
{
o.Key = "Y";
o.KeyModifiers = "Ctrl+Shift+Alt";
})
.Bind(context.Configuration.GetSection("Keyboard"))
.ValidateDataAnnotations()
.ValidateOnStart();
// Services
services.AddSingleton<IKeyboardHooks, GlobalKeyInterceptorKeyboardHooks>();
Expand Down
12 changes: 12 additions & 0 deletions src/ChatPrisma/Options/KeyboardOptions.cs
Original file line number Diff line number Diff line change
@@ -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!;
}
}
Original file line number Diff line number Diff line change
@@ -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<GlobalKeyInterceptorKeyboardHooks> logger) : IKeyboardHooks, IDisposable
public class GlobalKeyInterceptorKeyboardHooks(ILogger<GlobalKeyInterceptorKeyboardHooks> logger, IOptions<KeyboardOptions> 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);
Expand All @@ -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>(key, ignoreCase: true);

var parsedKeyModifiers = keyModifiers
.Split('+')
.Select(f => f.Trim())
.Select(f => Enum.Parse<KeyModifier>(f, ignoreCase: true))
.Aggregate((x, y) => x | y);

return (parsedKey, parsedKeyModifiers);
}
catch (Exception)
{
throw new PrismaException("Could not parse keyboard shortcut.");
}
}

public void Dispose()
Expand Down
4 changes: 4 additions & 0 deletions src/ChatPrisma/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
"CheckForUpdatesInBackground": true,
"MinutesBetweenUpdateChecks": 10
},
"Keyboard": {
"Key": "Y",
"KeyModifiers": "Ctrl+Shift+Alt",
},
"NLog": {
"throwConfigExceptions": true,
"targets": {
Expand Down

0 comments on commit 5583e5f

Please sign in to comment.