Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

ButtonClickEventTriggerBehavior misses previously pressed modifiers #154

Open
JaggerJo opened this issue Mar 19, 2024 · 0 comments
Open

Comments

@JaggerJo
Copy link

I had 2 problems with ButtonClickEventTriggerBehavior.

  1. It seems that only modifier keys get tracked for events that reach the control:

    AssociatedObject.AddHandler(InputElement.KeyDownEvent, Button_OnKeyDown, RoutingStrategies.Tunnel);
    AssociatedObject.AddHandler(InputElement.KeyUpEvent, Button_OnKeyUp, RoutingStrategies.Tunnel);

  2. Key Events that happened outside of the control / before the control was created are not respected.

I might be wrong about 1.

My working modified version of ButtonClickEventTriggerBehavior is below, but I'm not really happy with the implementation as it involves setting event handlers on the top-level.

Is there a better way to archive this?

public static class ModifierKeyStateProvider
{
    public static KeyModifiers ModifierKeyState { get; set; }
}

public class ButtonClickModifierKeyBehavior : Trigger<Button>
{
    public static readonly StyledProperty<KeyModifiers> KeyModifiersProperty =
        AvaloniaProperty.Register<ButtonClickModifierKeyBehavior, KeyModifiers>(nameof(KeyModifiers));

    public KeyModifiers KeyModifiers
    {
        get => GetValue(KeyModifiersProperty);
        set => SetValue(KeyModifiersProperty, value);
    }

    protected override void OnAttachedToVisualTree()
    {
        if (AssociatedObject is not null)
        {
            AssociatedObject.Click += AssociatedObject_OnClick;
        }
    }

    protected override void OnDetachedFromVisualTree()
    {
        if (AssociatedObject is not null)
        {
            AssociatedObject.Click -= AssociatedObject_OnClick;
        }
    }

    private void AssociatedObject_OnClick(object? sender, RoutedEventArgs e)
    {
        if (AssociatedObject is not null && KeyModifiers == ModifierKeyStateProvider.ModifierKeyState)
        {
            Interaction.ExecuteActions(AssociatedObject, Actions, e);
        }
    }
}

Glue code in App code behind:

        var topLevel = this.GetTopLevel();

        topLevel.AddHandler(
            InputElement.KeyDownEvent,
            (s, e) => ModifierKeyStateProvider.ModifierKeyState = e.KeyModifiers,
            handledEventsToo: true
        );

        topLevel.AddHandler(
            InputElement.KeyUpEvent,
            (s, e) => ModifierKeyStateProvider.ModifierKeyState = e.KeyModifiers,
            handledEventsToo: true
        );
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants