generated from jfversluis/Plugin.Maui.Feature
-
Notifications
You must be signed in to change notification settings - Fork 9
Windows - Enable preview events #9
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
Open
bengavin
wants to merge
5
commits into
davidortinau:main
Choose a base branch
from
CoreBTS:windows/enable-preview-events
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
45943af
UPDATE: Enable preview events on Windows
bengavin 77cf6b8
Tweak logic to prefer the incoming bindable as a Page
bengavin ccca65a
hook/unhook from platformView, not the bindable Handler which can be …
bengavin aee7098
REMOVE: Remove un-used Trigger classes
bengavin 6dc2335
UPDATE: Workaround bug in CommunityToolkit.PopupService
bengavin 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -1,85 +1,108 @@ | ||
#if WINDOWS | ||
using Microsoft.UI.Xaml; | ||
using Microsoft.UI.Xaml.Input; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Plugin.Maui.KeyListener; | ||
|
||
public partial class KeyboardBehavior : PlatformBehavior<VisualElement> | ||
{ | ||
protected override void OnAttachedTo(VisualElement bindable, FrameworkElement platformView) | ||
public KeyboardBehavior() | ||
{ | ||
base.OnAttachedTo(bindable, platformView); | ||
// We prefer preview events on Windows | ||
UsePreviewEvents = true; | ||
} | ||
|
||
var window = bindable.Window.Handler.PlatformView as Microsoft.UI.Xaml.Window; | ||
// See: https://github.com/CommunityToolkit/Maui/issues/1912 | ||
private List<Guid> _boundIds = [Guid.Empty]; | ||
|
||
window.Content.KeyDown += OnKeyDown; | ||
window.Content.KeyUp += OnKeyUp; | ||
window.Content.PreviewKeyDown += OnPreviewKeyDown; | ||
window.Content.PreviewKeyUp += OnPreviewKeyUp; | ||
protected override void OnAttachedTo(VisualElement bindable, FrameworkElement platformView) | ||
{ | ||
// Handle case where element is bound multiple times, we only allow a single binding for this behavior | ||
if (_boundIds.Contains(bindable?.Id ?? Guid.Empty)) { return; } | ||
_boundIds.Add(bindable.Id); | ||
|
||
//platformView.KeyDown += OnKeyDown; | ||
//platformView.KeyUp += OnKeyUp; | ||
//platformView.PreviewKeyDown += OnPreviewKeyDown; | ||
//platformView.PreviewKeyUp += OnPreviewKeyUp; | ||
if (UsePreviewEvents) | ||
{ | ||
platformView.PreviewKeyDown += OnPreviewKeyDown; | ||
platformView.PreviewKeyUp += OnPreviewKeyUp; | ||
} | ||
else | ||
{ | ||
platformView.KeyDown += OnKeyDown; | ||
platformView.KeyUp += OnKeyUp; | ||
} | ||
|
||
base.OnAttachedTo(bindable, platformView); | ||
} | ||
|
||
protected override void OnDetachedFrom(VisualElement bindable, FrameworkElement platformView) | ||
{ | ||
base.OnDetachedFrom(bindable, platformView); | ||
|
||
var window = bindable.Window.Handler.PlatformView as Microsoft.UI.Xaml.Window; | ||
if ((bindable?.Id ?? Guid.Empty) != Guid.Empty && _boundIds.Contains(bindable.Id)) { _boundIds.Remove(bindable.Id); } | ||
|
||
window.Content.KeyDown -= OnKeyDown; | ||
window.Content.KeyUp -= OnKeyUp; | ||
window.Content.PreviewKeyDown -= OnPreviewKeyDown; | ||
window.Content.PreviewKeyUp -= OnPreviewKeyUp; | ||
if (UsePreviewEvents) | ||
{ | ||
platformView.PreviewKeyDown -= OnPreviewKeyDown; | ||
platformView.PreviewKeyUp -= OnPreviewKeyUp; | ||
} | ||
else | ||
{ | ||
platformView.KeyDown -= OnKeyDown; | ||
platformView.KeyUp -= OnKeyUp; | ||
} | ||
|
||
//platformView.KeyDown -= OnKeyDown; | ||
//platformView.KeyUp -= OnKeyUp; | ||
//platformView.PreviewKeyDown -= OnPreviewKeyDown; | ||
//platformView.PreviewKeyUp -= OnPreviewKeyUp; | ||
base.OnDetachedFrom(bindable, platformView); | ||
} | ||
|
||
void OnWindowKeyDown(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e) | ||
void OnPreviewKeyDown(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e) | ||
{ | ||
Console.WriteLine($"OnWindowKeyDown {e.Key}"); | ||
char keyChar = ((char)e.Key); | ||
var eventArgs = new KeyPressedEventArgs | ||
{ | ||
Key = e.Key.ToKeyboardKey(), | ||
KeyChar = keyChar | ||
}; | ||
|
||
this.RaiseKeyDown(eventArgs); | ||
|
||
e.Handled = eventArgs.Handled; | ||
} | ||
|
||
void OnKeyDown(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e) | ||
{ | ||
char keyChar = ((char)e.Key); | ||
var eventArgs = new KeyPressedEventArgs | ||
{ | ||
Keys = e.Key.ToKeyboardKeys(), | ||
Key = e.Key.ToKeyboardKey(), | ||
KeyChar = keyChar | ||
}; | ||
|
||
this.RaiseKeyDown(eventArgs); | ||
|
||
e.Handled = eventArgs.Handled; | ||
} | ||
|
||
void OnPreviewKeyUp(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e) | ||
{ | ||
Console.WriteLine($"OnPreviewKeyUp {e.Key}"); | ||
} | ||
char keyChar = ((char)e.Key); | ||
var eventArgs = new KeyPressedEventArgs | ||
{ | ||
Key = e.Key.ToKeyboardKey(), | ||
KeyChar = keyChar | ||
}; | ||
|
||
void OnPreviewKeyDown(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e) | ||
{ | ||
Console.WriteLine($"OnPreviewKeyDown {e.Key}"); | ||
this.RaiseKeyUp(eventArgs); | ||
e.Handled = eventArgs.Handled; | ||
} | ||
|
||
void OnKeyUp(object sender, Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e) | ||
{ | ||
char keyChar = ((char)e.Key); | ||
var eventArgs = new KeyPressedEventArgs | ||
{ | ||
Keys = e.Key.ToKeyboardKeys(), | ||
Key = e.Key.ToKeyboardKey(), | ||
KeyChar = keyChar | ||
}; | ||
this.RaiseKeyUp(eventArgs); | ||
e.Handled = eventArgs.Handled; | ||
} | ||
} | ||
#endif |
This file contains hidden or 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 hidden or 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
39 changes: 0 additions & 39 deletions
39
src/Plugin.Maui.KeyListener/KeyboardBehaviorTrigger.Windows.cs
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
41 changes: 0 additions & 41 deletions
41
src/Plugin.Maui.KeyListener/KeyboardBehaviorTrigger.iOS.cs
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 |
---|---|---|
|
@@ -10,7 +10,6 @@ public enum KeyboardModifiers | |
Alt = 16 | ||
} | ||
|
||
[Flags] | ||
public enum KeyboardKeys | ||
{ | ||
None, | ||
|
Oops, something went wrong.
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.
The initialization of _boundIds using [Guid.Empty] is not valid C# syntax; consider changing it to 'new List { Guid.Empty }' if the intention is to start with a default value, or initialize an empty list if no default is required.
Copilot uses AI. Check for mistakes.