using System.Reactive.Disposables; using Avalonia.Input; using Avalonia.Interactivity; using Avalonia.Xaml.Interactivity; namespace Avalonia.Xaml.Interactions.Custom; /// /// /// public class KeyDownTrigger : RoutedEventTriggerBase { /// /// /// public static readonly StyledProperty KeyProperty = AvaloniaProperty.Register(nameof(Key)); /// /// /// public Key Key { get => GetValue(KeyProperty); set => SetValue(KeyProperty, value); } /// /// /// public bool MarkAsHandled { get; set; } /// /// /// /// protected override void OnAttached(CompositeDisposable disposables) { if (AssociatedObject is InputElement element) { var disposable = element.AddDisposableHandler(InputElement.KeyDownEvent, OnKeyDown, EventRoutingStrategy); disposables.Add(disposable); } } private void OnKeyDown(object? sender, KeyEventArgs e) { if (!IsEnabled) { return; } if (e.Key == Key) { e.Handled = MarkAsHandled; Interaction.ExecuteActions(AssociatedObject, Actions, null); } } }