using System.Reactive.Disposables; using Avalonia.Input; using Avalonia.Interactivity; namespace Avalonia.Xaml.Interactions.Custom; /// /// /// public class ExecuteCommandOnKeyDownBehavior : ExecuteCommandOnKeyBehaviorBase { /// /// /// /// protected override void OnAttachedToVisualTree(CompositeDisposable disposable) { var control = SourceControl ?? AssociatedObject; var dispose = control? .AddDisposableHandler( InputElement.KeyDownEvent, OnKeyDown, EventRoutingStrategy); if (dispose is not null) { disposable.Add(dispose); } } private void OnKeyDown(object? sender, KeyEventArgs e) { var haveKey = Key is not null && e.Key == Key; var haveGesture = Gesture is not null && Gesture.Matches(e); if (!haveKey && !haveGesture) { return; } if (e.Handled) { return; } if (ExecuteCommand()) { e.Handled = MarkAsHandled; } } }