-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New behavior KeyDownTriggerBehavior #4160
Merged
michael-hawker
merged 11 commits into
CommunityToolkit:main
from
ArchieCoder:feature/KeyDownTriggerBehavior
Aug 13, 2021
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f9541bf
Completed
ArchieCoder 7513e42
Update Microsoft.Toolkit.Uwp.UI.Behaviors/Keyboard/KeyDownTriggerBeha…
ArchieCoder fc4e6d7
Update Microsoft.Toolkit.Uwp.UI.Behaviors/Keyboard/KeyDownTriggerBeha…
ArchieCoder 6c5f8a2
Fixes post code review
ArchieCoder 368b8a0
Merge branch 'feature/KeyDownTriggerBehavior' of https://github.com/A…
ArchieCoder b1905ce
Update Microsoft.Toolkit.Uwp.SampleApp/SamplePages/samples.json
ArchieCoder 3f0df53
Fix
ArchieCoder e2e5a65
Added license
ArchieCoder 8b3be50
Fix StyleCop
ArchieCoder eab041e
Updated doc URL to more relevant filename
XAML-Knight bd052bb
Merge branch 'local/test_changes_2' into local/KDTB_change
XAML-Knight File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
....Toolkit.Uwp.SampleApp/SamplePages/KeyDownTriggerBehavior/KeyDownTriggerBehaviorXaml.bind
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:ani="using:Microsoft.Toolkit.Uwp.UI.Animations" | ||
xmlns:behaviors="using:Microsoft.Toolkit.Uwp.UI.Behaviors" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:interactivity="using:Microsoft.Xaml.Interactivity" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
mc:Ignorable="d"> | ||
|
||
<StackPanel | ||
VerticalAlignment="Center"> | ||
|
||
<TextBox | ||
PlaceholderText="Set the focus to this TextBox and press enter to trigger the animation" | ||
Width="500"> | ||
<interactivity:Interaction.Behaviors> | ||
<behaviors:KeyDownTriggerBehavior | ||
Key="Enter"> | ||
<behaviors:StartAnimationAction Animation="{Binding ElementName=MoveAnimation}" /> | ||
</behaviors:KeyDownTriggerBehavior> | ||
</interactivity:Interaction.Behaviors> | ||
</TextBox> | ||
|
||
<Button Background="Gray" Margin="0,40,0,0" Width="200" Height="200" HorizontalAlignment="Center" VerticalAlignment="Center"> | ||
<ani:Explicit.Animations> | ||
<ani:AnimationSet x:Name="MoveAnimation" IsSequential="True"> | ||
<ani:TranslationAnimation Duration="0:0:3" To="0,32,0" From="0,0,0" /> | ||
<ani:StartAnimationActivity Delay="0:0:3" Animation="{Binding ElementName=FadeOutAnimation}"/> | ||
<ani:StartAnimationActivity Delay="0:0:3" Animation="{Binding ElementName=FadeInAnimation}"/> | ||
<ani:TranslationAnimation Duration="0:0:1" To="0,0,0" From="0,32,0" /> | ||
</ani:AnimationSet> | ||
</ani:Explicit.Animations> | ||
|
||
<Image Source="ms-appx:///Assets/ToolkitLogo.png" Height="100" Width="100"> | ||
<ani:Explicit.Animations> | ||
<ani:AnimationSet x:Name="FadeOutAnimation"> | ||
<ani:OpacityAnimation From="1" | ||
To="0" | ||
Duration="0:0:1" | ||
Delay="0" | ||
EasingType="Linear" | ||
EasingMode="EaseOut"/> | ||
</ani:AnimationSet> | ||
<ani:AnimationSet x:Name="FadeInAnimation"> | ||
<ani:OpacityAnimation From="0" | ||
To="1" | ||
Duration="0:0:1" | ||
Delay="0" | ||
EasingType="Linear" | ||
EasingMode="EaseOut"/> | ||
</ani:AnimationSet> | ||
</ani:Explicit.Animations> | ||
</Image> | ||
|
||
</Button> | ||
|
||
</StackPanel> | ||
|
||
</Page> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -785,7 +785,7 @@ | |
"Type": "ThemeListenerPage", | ||
"Subcategory": "Systems", | ||
"About": "The ThemeListener allows you to keep track of changes to the System Theme.", | ||
"CodeUrl" : "https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/master/Microsoft.Toolkit.Uwp.UI/Helpers/ThemeListener.cs", | ||
"CodeUrl": "https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/master/Microsoft.Toolkit.Uwp.UI/Helpers/ThemeListener.cs", | ||
"Icon": "/Assets/Helpers.png", | ||
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/helpers/ThemeListener.md" | ||
}, | ||
|
@@ -839,12 +839,21 @@ | |
"Icon": "/Assets/Helpers.png", | ||
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/behaviors/AutoSelectBehavior.md" | ||
}, | ||
{ | ||
"Name": "KeyDownTriggerBehavior", | ||
"Subcategory": "Systems", | ||
"About": "Behavior to listen to a key press on a control and executes actions", | ||
"CodeUrl": "https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/master/Microsoft.Toolkit.Uwp.UI.Behaviors/Keyboard/KeyDownTriggerBehavior.cs", | ||
"XamlCodeFile": "/SamplePages/KeyDownTriggerBehavior/KeyDownTriggerBehaviorXaml.bind", | ||
"Icon": "/Assets/Helpers.png", | ||
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/behaviors/KeyDownTriggerBehavior.md" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. DocumentationUrl modified, to fit more descriptive markdown file name |
||
}, | ||
{ | ||
"Name": "Win2d Path Mini Language Parser", | ||
"Type": "CanvasPathGeometryPage", | ||
"Subcategory": "Parser", | ||
"About": "CanvasPathGeometry class allows you to convert Win2d Path Mini Language string to CanvasGeometry, Brushes, CanvasStrokes or CanvasStrokeStyles.", | ||
"CodeUrl" : "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.Uwp.UI.Media/Geometry", | ||
"CodeUrl": "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.Uwp.UI.Media/Geometry", | ||
"Icon": "/SamplePages/CanvasPathGeometry/CanvasPathGeometry.png", | ||
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/parsers/CanvasPathGeometry.md" | ||
}, | ||
|
@@ -882,15 +891,15 @@ | |
"Name": "Guard APIs", | ||
"Subcategory": "Developer", | ||
"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.", | ||
"CodeUrl" : "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.Diagnostics", | ||
"CodeUrl": "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.Diagnostics", | ||
"Icon": "/Assets/Helpers.png", | ||
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/developer-tools/Guard.md" | ||
}, | ||
{ | ||
"Name": "High Performance APIs", | ||
"Subcategory": "Developer", | ||
"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.", | ||
"CodeUrl" : "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.HighPerformance", | ||
"CodeUrl": "https://github.com/CommunityToolkit/WindowsCommunityToolkit/tree/main/Microsoft.Toolkit.HighPerformance", | ||
"Icon": "/Assets/Helpers.png", | ||
"DocumentationUrl": "https://raw.githubusercontent.com/MicrosoftDocs/WindowsCommunityToolkitDocs/master/docs/high-performance/Introduction.md" | ||
}, | ||
|
62 changes: 62 additions & 0 deletions
62
Microsoft.Toolkit.Uwp.UI.Behaviors/Keyboard/KeyDownTriggerBehavior.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using Microsoft.Xaml.Interactivity; | ||
using Windows.System; | ||
using Windows.UI.Xaml; | ||
using Windows.UI.Xaml.Input; | ||
|
||
namespace Microsoft.Toolkit.Uwp.UI.Behaviors | ||
{ | ||
/// <summary> | ||
/// This behavior listens to a key down event on the associated <see cref="UIElement"/> when it is loaded and executes an action. | ||
/// </summary> | ||
[TypeConstraint(typeof(FrameworkElement))] | ||
public class KeyDownTriggerBehavior : Trigger<FrameworkElement> | ||
{ | ||
/// <summary> | ||
/// Identifies the <see cref="Key"/> property. | ||
/// </summary> | ||
public static readonly DependencyProperty KeyProperty = DependencyProperty.Register( | ||
nameof(Key), | ||
typeof(VirtualKey), | ||
typeof(KeyDownTriggerBehavior), | ||
new PropertyMetadata(null)); | ||
|
||
/// <summary> | ||
/// Gets or sets the key to listen when the associated object is loaded. | ||
/// </summary> | ||
public VirtualKey Key | ||
{ | ||
get => (VirtualKey)GetValue(KeyProperty); | ||
set => SetValue(KeyProperty, value); | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override void OnAttached() | ||
{ | ||
((FrameworkElement)AssociatedObject).KeyDown += OnAssociatedObjectKeyDown; | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override void OnDetaching() | ||
{ | ||
((FrameworkElement)AssociatedObject).KeyDown -= OnAssociatedObjectKeyDown; | ||
} | ||
|
||
/// <summary> | ||
/// Invokes the current actions when the <see cref="Key"/> is pressed. | ||
/// </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) | ||
{ | ||
if (keyRoutedEventArgs.Key == Key) | ||
ArchieCoder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
keyRoutedEventArgs.Handled = true; | ||
Interaction.ExecuteActions(sender, Actions, keyRoutedEventArgs); | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove this to minimize noise.