Add support for custom access keys
This commit is contained in:
parent
aa7c159985
commit
5d0937dc48
69
Source/LibationAvalonia/AccessKeyHandlerEx.cs
Normal file
69
Source/LibationAvalonia/AccessKeyHandlerEx.cs
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -2,6 +2,7 @@
|
|||||||
using Avalonia;
|
using Avalonia;
|
||||||
using Avalonia.Controls;
|
using Avalonia.Controls;
|
||||||
using Avalonia.Controls.ApplicationLifetimes;
|
using Avalonia.Controls.ApplicationLifetimes;
|
||||||
|
using Avalonia.Input;
|
||||||
using Avalonia.Markup.Xaml;
|
using Avalonia.Markup.Xaml;
|
||||||
using Avalonia.Media;
|
using Avalonia.Media;
|
||||||
using Avalonia.Platform;
|
using Avalonia.Platform;
|
||||||
@ -44,6 +45,9 @@ namespace LibationAvalonia
|
|||||||
{
|
{
|
||||||
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
|
||||||
{
|
{
|
||||||
|
var acceleratorKey = Configuration.IsMacOs ? KeyModifiers.Meta : KeyModifiers.Alt;
|
||||||
|
AvaloniaLocator.CurrentMutable.Bind<IAccessKeyHandler>().ToFunc(() => new AccessKeyHandlerEx(acceleratorKey));
|
||||||
|
|
||||||
var config = Configuration.Instance;
|
var config = Configuration.Instance;
|
||||||
|
|
||||||
if (!config.LibationSettingsAreValid)
|
if (!config.LibationSettingsAreValid)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user