Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 109 additions & 16 deletions components/Behaviors/src/Keyboard/KeyDownTriggerBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,146 @@
namespace CommunityToolkit.WinUI.Behaviors;

/// <summary>
/// This behavior listens to a key down event on the associated <see cref="UIElement"/> when it is loaded and executes an action.
/// A behavior that listens to <see cref="UIElement.PreviewKeyDown"/> on the associated
/// <see cref="FrameworkElement"/> and executes its actions when the specified key and
/// optional modifier keys are pressed. Supports capturing handled events.
/// </summary>
[TypeConstraint(typeof(FrameworkElement))]
public class KeyDownTriggerBehavior : Trigger<FrameworkElement>
{
private KeyEventHandler? _handler;
Comment thread
Irina-Konovalova marked this conversation as resolved.

/// <summary>
/// Identifies the <see cref="Key"/> property.
/// Identifies the <see cref="Key"/> dependency property.
/// </summary>
public static readonly DependencyProperty KeyProperty = DependencyProperty.Register(
nameof(Key),
typeof(VirtualKey),
typeof(KeyDownTriggerBehavior),
new PropertyMetadata(null));
public static readonly DependencyProperty KeyProperty =
DependencyProperty.Register(
nameof(Key),
typeof(VirtualKey),
typeof(KeyDownTriggerBehavior),
new PropertyMetadata(VirtualKey.None));

/// <summary>
/// Gets or sets the key to listen when the associated object is loaded.
/// Gets or sets the key that triggers the behavior.
/// </summary>
public VirtualKey Key
{
get => (VirtualKey)GetValue(KeyProperty);
set => SetValue(KeyProperty, value);
}

/// <summary>
/// Identifies the <see cref="Modifiers"/> dependency property.
/// </summary>
public static readonly DependencyProperty ModifiersProperty =
DependencyProperty.Register(
nameof(Modifiers),
typeof(VirtualKeyModifiers),
typeof(KeyDownTriggerBehavior),
new PropertyMetadata(VirtualKeyModifiers.None));

/// <summary>
/// Gets or sets the modifier keys that must be pressed together with <see cref="Key"/>.
/// </summary>
public VirtualKeyModifiers Modifiers
{
get => (VirtualKeyModifiers)GetValue(ModifiersProperty);
set => SetValue(ModifiersProperty, value);
}

/// <summary>
/// Identifies the <see cref="HandledEventsToo"/> dependency property.
/// </summary>
public static readonly DependencyProperty HandledEventsTooProperty =
DependencyProperty.Register(
nameof(HandledEventsToo),
typeof(bool),
typeof(KeyDownTriggerBehavior),
new PropertyMetadata(true));

/// <summary>
/// Gets or sets a value indicating whether the behavior should receive
/// <see cref="UIElement.PreviewKeyDown"/> events even if they were already handled.
/// </summary>
public bool HandledEventsToo
{
get => (bool)GetValue(HandledEventsTooProperty);
set => SetValue(HandledEventsTooProperty, value);
}

/// <inheritdoc/>
protected override void OnAttached()
{
AssociatedObject.KeyDown += OnAssociatedObjectKeyDown;
_handler = OnPreviewKeyDown;

AssociatedObject.AddHandler(
UIElement.PreviewKeyDownEvent,
_handler,
HandledEventsToo);
}

/// <inheritdoc/>
protected override void OnDetaching()
{
AssociatedObject.KeyDown -= OnAssociatedObjectKeyDown;
if (_handler is not null)
{
AssociatedObject.RemoveHandler(
UIElement.PreviewKeyDownEvent,
_handler);

_handler = null;
}
}

/// <summary>
/// Invokes the current actions when the <see cref="Key"/> is pressed.
/// Handles the <see cref="UIElement.PreviewKeyDown"/> event and executes the associated actions
/// when the specified <see cref="Key"/> and <see cref="Modifiers"/> match.
/// </summary>
/// <param name="sender">The source <see cref="UIElement"/> instance.</param>
/// <param name="keyRoutedEventArgs">The arguments for the event (unused).</param>
private void OnAssociatedObjectKeyDown(object sender, KeyRoutedEventArgs keyRoutedEventArgs)
private void OnPreviewKeyDown(object sender, KeyRoutedEventArgs keyRoutedEventArgs)
{
if (keyRoutedEventArgs.Key == Key)
if (keyRoutedEventArgs.Key != Key)
{
return;
}

if (!CheckModifiers())

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Upon further consideration, this is actually a breaking change. In order to retain the existing behavior, we need to introduce a new property that declares whether or not to check for modifiers. Its default value needs to be false.

{
keyRoutedEventArgs.Handled = true;
Interaction.ExecuteActions(sender, Actions, keyRoutedEventArgs);
return;
}

keyRoutedEventArgs.Handled = true;
Interaction.ExecuteActions(sender, Actions, keyRoutedEventArgs);
}
}

/// <summary>
/// Checks whether all required modifier keys specified in <see cref="Modifiers"/>
/// are currently pressed. Retrieves the physical key states once and evaluates
/// them against the required modifier flags.
/// </summary>
/// <returns><see langword="true"/> if the current modifier state matches the requirements; otherwise, <see langword="false"/>.</returns>
private bool CheckModifiers()
{
bool ctrl = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Control).HasFlag(CoreVirtualKeyStates.Down);
bool shift = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Shift).HasFlag(CoreVirtualKeyStates.Down);
bool alt = InputKeyboardSource.GetKeyStateForCurrentThread(VirtualKey.Menu).HasFlag(CoreVirtualKeyStates.Down);

return Match(VirtualKeyModifiers.Control, ctrl)
&& Match(VirtualKeyModifiers.Shift, shift)
&& Match(VirtualKeyModifiers.Menu, alt);
}

/// <summary>
/// Compares whether a specific modifier flag is required and whether the
/// corresponding key is currently pressed.
/// </summary>
/// <param name="mod">The modifier flag to evaluate.</param>
/// <param name="isDown">The current physical key state for that modifier.</param>
/// <returns><see langword="true"/> if the requirement matches the key state; otherwise, <see langword="false"/>.</returns>
private bool Match(VirtualKeyModifiers mod, bool isDown)
{
bool required = (Modifiers & mod) != 0;
return required == isDown;
}
}