Skip to content

Commit bd71319

Browse files
Merge pull request #4160 from ArchieCoder/feature/KeyDownTriggerBehavior
New behavior KeyDownTriggerBehavior
2 parents ab5b03d + bd052bb commit bd71319

File tree

5 files changed

+137
-4
lines changed

5 files changed

+137
-4
lines changed

Microsoft.Toolkit.Uwp.SampleApp/Microsoft.Toolkit.Uwp.SampleApp.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@
626626
<Content Include="SamplePages\Primitives\ConstrainedBox.bind">
627627
<SubType>Designer</SubType>
628628
</Content>
629+
<Content Include="SamplePages\KeyDownTriggerBehavior\KeyDownTriggerBehaviorXaml.bind" />
629630
</ItemGroup>
630631
<ItemGroup>
631632
<Compile Include="App.xaml.cs">
@@ -1475,6 +1476,7 @@
14751476
<Name>Visual C++ 2015 Runtime for Universal Windows Platform Apps</Name>
14761477
</SDKReference>
14771478
</ItemGroup>
1479+
<ItemGroup />
14781480
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
14791481
<VisualStudioVersion>14.0</VisualStudioVersion>
14801482
</PropertyGroup>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:ani="using:Microsoft.Toolkit.Uwp.UI.Animations"
4+
xmlns:behaviors="using:Microsoft.Toolkit.Uwp.UI.Behaviors"
5+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6+
xmlns:interactivity="using:Microsoft.Xaml.Interactivity"
7+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
8+
mc:Ignorable="d">
9+
10+
<StackPanel
11+
VerticalAlignment="Center">
12+
13+
<TextBox
14+
PlaceholderText="Set the focus to this TextBox and press enter to trigger the animation"
15+
Width="500">
16+
<interactivity:Interaction.Behaviors>
17+
<behaviors:KeyDownTriggerBehavior
18+
Key="Enter">
19+
<behaviors:StartAnimationAction Animation="{Binding ElementName=MoveAnimation}" />
20+
</behaviors:KeyDownTriggerBehavior>
21+
</interactivity:Interaction.Behaviors>
22+
</TextBox>
23+
24+
<Button Background="Gray" Margin="0,40,0,0" Width="200" Height="200" HorizontalAlignment="Center" VerticalAlignment="Center">
25+
<ani:Explicit.Animations>
26+
<ani:AnimationSet x:Name="MoveAnimation" IsSequential="True">
27+
<ani:TranslationAnimation Duration="0:0:3" To="0,32,0" From="0,0,0" />
28+
<ani:StartAnimationActivity Delay="0:0:3" Animation="{Binding ElementName=FadeOutAnimation}"/>
29+
<ani:StartAnimationActivity Delay="0:0:3" Animation="{Binding ElementName=FadeInAnimation}"/>
30+
<ani:TranslationAnimation Duration="0:0:1" To="0,0,0" From="0,32,0" />
31+
</ani:AnimationSet>
32+
</ani:Explicit.Animations>
33+
34+
<Image Source="ms-appx:///Assets/ToolkitLogo.png" Height="100" Width="100">
35+
<ani:Explicit.Animations>
36+
<ani:AnimationSet x:Name="FadeOutAnimation">
37+
<ani:OpacityAnimation From="1"
38+
To="0"
39+
Duration="0:0:1"
40+
Delay="0"
41+
EasingType="Linear"
42+
EasingMode="EaseOut"/>
43+
</ani:AnimationSet>
44+
<ani:AnimationSet x:Name="FadeInAnimation">
45+
<ani:OpacityAnimation From="0"
46+
To="1"
47+
Duration="0:0:1"
48+
Delay="0"
49+
EasingType="Linear"
50+
EasingMode="EaseOut"/>
51+
</ani:AnimationSet>
52+
</ani:Explicit.Animations>
53+
</Image>
54+
55+
</Button>
56+
57+
</StackPanel>
58+
59+
</Page>

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/XamlOnlyPage.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<triggers:UserHandPreferenceStateTrigger x:Key="UserHandPreferenceStateTrigger" />
3333
<triggers:UserInteractionModeStateTrigger x:Key="UserInteractionModeStateTrigger" />
3434
<behaviors:StartAnimationAction x:Key="StartAnimationAction" />
35+
<behaviors:KeyDownTriggerBehavior x:Key="KeyDownTriggerBehavior" />
3536
<behaviors:AutoSelectBehavior x:Key="AutoSelectBehavior" />
3637
<controls:ColorPicker x:Key="ColorPicker" />
3738
<controls:ColorPickerButton x:Key="ColorPickerButton" />

Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@
785785
"Type": "ThemeListenerPage",
786786
"Subcategory": "Systems",
787787
"About": "The ThemeListener allows you to keep track of changes to the System Theme.",
788-
"CodeUrl" : "https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/master/Microsoft.Toolkit.Uwp.UI/Helpers/ThemeListener.cs",
788+
"CodeUrl": "https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/master/Microsoft.Toolkit.Uwp.UI/Helpers/ThemeListener.cs",
789789
"Icon": "/Assets/Helpers.png",
790790
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/helpers/ThemeListener.md"
791791
},
@@ -839,12 +839,21 @@
839839
"Icon": "/Assets/Helpers.png",
840840
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/behaviors/AutoSelectBehavior.md"
841841
},
842+
{
843+
"Name": "KeyDownTriggerBehavior",
844+
"Subcategory": "Systems",
845+
"About": "Behavior to listen to a key press on a control and executes actions",
846+
"CodeUrl": "https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/master/Microsoft.Toolkit.Uwp.UI.Behaviors/Keyboard/KeyDownTriggerBehavior.cs",
847+
"XamlCodeFile": "/SamplePages/KeyDownTriggerBehavior/KeyDownTriggerBehaviorXaml.bind",
848+
"Icon": "/Assets/Helpers.png",
849+
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/behaviors/KeyDownTriggerBehavior.md"
850+
},
842851
{
843852
"Name": "Win2d Path Mini Language Parser",
844853
"Type": "CanvasPathGeometryPage",
845854
"Subcategory": "Parser",
846855
"About": "CanvasPathGeometry class allows you to convert Win2d Path Mini Language string to CanvasGeometry, Brushes, CanvasStrokes or CanvasStrokeStyles.",
847-
"CodeUrl" : "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.Uwp.UI.Media/Geometry",
856+
"CodeUrl": "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.Uwp.UI.Media/Geometry",
848857
"Icon": "/SamplePages/CanvasPathGeometry/CanvasPathGeometry.png",
849858
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/parsers/CanvasPathGeometry.md"
850859
},
@@ -882,15 +891,15 @@
882891
"Name": "Guard APIs",
883892
"Subcategory": "Developer",
884893
"About": "The Guard APIs can be used to validate method arguments in a streamlined manner, which is also faster, less verbose, more expressive and less error prone than manually writing checks and throwing exceptions.",
885-
"CodeUrl" : "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.Diagnostics",
894+
"CodeUrl": "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.Diagnostics",
886895
"Icon": "/Assets/Helpers.png",
887896
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/developer-tools/Guard.md"
888897
},
889898
{
890899
"Name": "High Performance APIs",
891900
"Subcategory": "Developer",
892901
"About": "The High Performance package contains a set of APIs that are heavily focused on optimization. All the new APIs have been carefully crafted to achieve the best possible performance when using them, either through reduced memory allocation, micro-optimizations at the assembly level, or by structuring the APIs in a way that facilitates writing performance oriented code in general.",
893-
"CodeUrl" : "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.HighPerformance",
902+
"CodeUrl": "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.HighPerformance",
894903
"Icon": "/Assets/Helpers.png",
895904
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/high-performance/Introduction.md"
896905
},
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Microsoft.Xaml.Interactivity;
6+
using Windows.System;
7+
using Windows.UI.Xaml;
8+
using Windows.UI.Xaml.Input;
9+
10+
namespace Microsoft.Toolkit.Uwp.UI.Behaviors
11+
{
12+
/// <summary>
13+
/// This behavior listens to a key down event on the associated <see cref="UIElement"/> when it is loaded and executes an action.
14+
/// </summary>
15+
[TypeConstraint(typeof(FrameworkElement))]
16+
public class KeyDownTriggerBehavior : Trigger<FrameworkElement>
17+
{
18+
/// <summary>
19+
/// Identifies the <see cref="Key"/> property.
20+
/// </summary>
21+
public static readonly DependencyProperty KeyProperty = DependencyProperty.Register(
22+
nameof(Key),
23+
typeof(VirtualKey),
24+
typeof(KeyDownTriggerBehavior),
25+
new PropertyMetadata(null));
26+
27+
/// <summary>
28+
/// Gets or sets the key to listen when the associated object is loaded.
29+
/// </summary>
30+
public VirtualKey Key
31+
{
32+
get => (VirtualKey)GetValue(KeyProperty);
33+
set => SetValue(KeyProperty, value);
34+
}
35+
36+
/// <inheritdoc/>
37+
protected override void OnAttached()
38+
{
39+
((FrameworkElement)AssociatedObject).KeyDown += OnAssociatedObjectKeyDown;
40+
}
41+
42+
/// <inheritdoc/>
43+
protected override void OnDetaching()
44+
{
45+
((FrameworkElement)AssociatedObject).KeyDown -= OnAssociatedObjectKeyDown;
46+
}
47+
48+
/// <summary>
49+
/// Invokes the current actions when the <see cref="Key"/> is pressed.
50+
/// </summary>
51+
/// <param name="sender">The source <see cref="UIElement"/> instance.</param>
52+
/// <param name="keyRoutedEventArgs">The arguments for the event (unused).</param>
53+
private void OnAssociatedObjectKeyDown(object sender, KeyRoutedEventArgs keyRoutedEventArgs)
54+
{
55+
if (keyRoutedEventArgs.Key == Key)
56+
{
57+
keyRoutedEventArgs.Handled = true;
58+
Interaction.ExecuteActions(sender, Actions, keyRoutedEventArgs);
59+
}
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)