From 5d0937dc4871aff77c9885064521fa240d279412 Mon Sep 17 00:00:00 2001 From: MBucari Date: Wed, 29 Mar 2023 19:44:56 -0600 Subject: [PATCH] Add support for custom access keys --- Source/LibationAvalonia/AccessKeyHandlerEx.cs | 69 +++++++++++++++++++ Source/LibationAvalonia/App.axaml.cs | 4 ++ 2 files changed, 73 insertions(+) create mode 100644 Source/LibationAvalonia/AccessKeyHandlerEx.cs diff --git a/Source/LibationAvalonia/AccessKeyHandlerEx.cs b/Source/LibationAvalonia/AccessKeyHandlerEx.cs new file mode 100644 index 00000000..b60d3ea5 --- /dev/null +++ b/Source/LibationAvalonia/AccessKeyHandlerEx.cs @@ -0,0 +1,69 @@ +using Avalonia; +using Avalonia.Input; +using System.Linq; + +namespace LibationAvalonia +{ + internal class AccessKeyHandlerEx : AccessKeyHandler + { + public KeyModifiers KeyModifier { get; } + private readonly Key[] ActivatorKeys; + + public AccessKeyHandlerEx(KeyModifiers menuKeyModifier) + { + KeyModifier = menuKeyModifier; + ActivatorKeys = menuKeyModifier switch + { + KeyModifiers.Alt => new[] { Key.LeftAlt, Key.RightAlt }, + KeyModifiers.Control => new[] { Key.LeftCtrl, Key.RightCtrl }, + KeyModifiers.Meta => new[] { Key.LWin, Key.RWin }, + _ => throw new System.NotSupportedException($"{nameof(KeyModifiers)}.{menuKeyModifier} is not implemented"), + }; + } + + protected override void OnPreviewKeyDown(object sender, KeyEventArgs e) + { + if (ActivatorKeys.Contains(e.Key)) + { + var newArgs = new KeyEventArgs + { + Key = Key.LeftAlt, + Handled = e.Handled, + KeyModifiers = e.KeyModifiers, + }; + base.OnPreviewKeyDown(sender, newArgs); + e.Handled = newArgs.Handled; + } + } + + protected override void OnPreviewKeyUp(object sender, KeyEventArgs e) + { + if (ActivatorKeys.Contains(e.Key)) + { + var newArgs = new KeyEventArgs() + { + Key = Key.LeftAlt, + Handled = e.Handled, + KeyModifiers = e.KeyModifiers, + }; + base.OnPreviewKeyUp(sender, newArgs); + e.Handled = newArgs.Handled; + } + } + + protected override void OnKeyDown(object sender, KeyEventArgs e) + { + if (e.KeyModifiers.HasAllFlags(KeyModifier)) + { + var newArgs = new KeyEventArgs + { + Key = e.Key, + Handled = e.Handled, + KeyModifiers = KeyModifiers.Alt, + }; + base.OnKeyDown(sender, newArgs); + e.Handled = newArgs.Handled; + } + } + } +} diff --git a/Source/LibationAvalonia/App.axaml.cs b/Source/LibationAvalonia/App.axaml.cs index e31714b4..c5e309ea 100644 --- a/Source/LibationAvalonia/App.axaml.cs +++ b/Source/LibationAvalonia/App.axaml.cs @@ -2,6 +2,7 @@ using Avalonia; using Avalonia.Controls; using Avalonia.Controls.ApplicationLifetimes; +using Avalonia.Input; using Avalonia.Markup.Xaml; using Avalonia.Media; using Avalonia.Platform; @@ -44,6 +45,9 @@ namespace LibationAvalonia { if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) { + var acceleratorKey = Configuration.IsMacOs ? KeyModifiers.Meta : KeyModifiers.Alt; + AvaloniaLocator.CurrentMutable.Bind().ToFunc(() => new AccessKeyHandlerEx(acceleratorKey)); + var config = Configuration.Instance; if (!config.LibationSettingsAreValid)