ButtonClickEventTriggerBehavior misses previously pressed modifiers
JaggerJo opened this issue · 0 comments
JaggerJo commented
I had 2 problems with ButtonClickEventTriggerBehavior
.
-
It seems that only modifier keys get tracked for events that reach the control:
-
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
);