diff --git a/src/Plugin.Maui.KeyListener/KeyPressedEventArgs.cs b/src/Plugin.Maui.KeyListener/KeyPressedEventArgs.cs index 3c363be..8faebc1 100644 --- a/src/Plugin.Maui.KeyListener/KeyPressedEventArgs.cs +++ b/src/Plugin.Maui.KeyListener/KeyPressedEventArgs.cs @@ -3,7 +3,8 @@ public sealed class KeyPressedEventArgs : EventArgs { public KeyboardModifiers Modifiers { get; internal set; } - public KeyboardKeys Keys { get; internal set; } + public KeyboardKeys Key { get; internal set; } public char KeyChar { get; internal set; } + public bool Handled { get; set; } } \ No newline at end of file diff --git a/src/Plugin.Maui.KeyListener/KeyboardBehavior.Windows.cs b/src/Plugin.Maui.KeyListener/KeyboardBehavior.Windows.cs index 1faccb9..6f2f759 100644 --- a/src/Plugin.Maui.KeyListener/KeyboardBehavior.Windows.cs +++ b/src/Plugin.Maui.KeyListener/KeyboardBehavior.Windows.cs @@ -1,53 +1,69 @@ #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 { - 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 _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) @@ -55,20 +71,26 @@ 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) @@ -76,10 +98,11 @@ 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 \ No newline at end of file diff --git a/src/Plugin.Maui.KeyListener/KeyboardBehavior.cs b/src/Plugin.Maui.KeyListener/KeyboardBehavior.cs index 0bc9a48..4aea163 100644 --- a/src/Plugin.Maui.KeyListener/KeyboardBehavior.cs +++ b/src/Plugin.Maui.KeyListener/KeyboardBehavior.cs @@ -1,16 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Plugin.Maui.KeyListener +namespace Plugin.Maui.KeyListener { public partial class KeyboardBehavior : PlatformBehavior { - KeyboardBehaviorTriggers _triggers; - - public KeyboardBehaviorTriggers Triggers => _triggers ??= new KeyboardBehaviorTriggers(); + public bool UsePreviewEvents { get; set; } public event EventHandler KeyDown; public event EventHandler KeyUp; diff --git a/src/Plugin.Maui.KeyListener/KeyboardBehavior.iOS.cs b/src/Plugin.Maui.KeyListener/KeyboardBehavior.iOS.cs index 0c3daa1..467de04 100644 --- a/src/Plugin.Maui.KeyListener/KeyboardBehavior.iOS.cs +++ b/src/Plugin.Maui.KeyListener/KeyboardBehavior.iOS.cs @@ -1,10 +1,4 @@ #if MACCATALYST || IOS -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Foundation; using UIKit; namespace Plugin.Maui.KeyListener diff --git a/src/Plugin.Maui.KeyListener/KeyboardBehaviorTrigger.Windows.cs b/src/Plugin.Maui.KeyListener/KeyboardBehaviorTrigger.Windows.cs deleted file mode 100644 index 4d83a66..0000000 --- a/src/Plugin.Maui.KeyListener/KeyboardBehaviorTrigger.Windows.cs +++ /dev/null @@ -1,39 +0,0 @@ -#if WINDOWS - -namespace Plugin.Maui.KeyListener; - -public sealed partial class KeyboardBehaviorTrigger -{ - void SetPlatformModifiers(KeyboardModifiers modifiers) - => throw new NotImplementedException(); - - void SetPlatformKeys(KeyboardKeys keys) - => throw new NotImplementedException(); -} - -internal class KeyboardBehaviorTriggerComparer : IEqualityComparer -{ - public bool Equals(KeyboardBehaviorTrigger x, KeyboardBehaviorTrigger y) - { - throw new NotImplementedException(); - //if (x == null && y == null) - // return true; - //if (x == null || y == null) - // return false; - - //return x.PlatformKeys.SequenceEqual(y.PlatformKeys) && x.PlatformModifiers == y.PlatformModifiers; - } - - public int GetHashCode(KeyboardBehaviorTrigger obj) - { - throw new NotImplementedException(); - //unchecked - //{ - // int hash = 17; - // hash = hash * 23 + obj.Keys.GetHashCode(); - // hash = hash * 23 + obj.PlatformModifiers.GetHashCode(); - // return hash; - //} - } -} -#endif \ No newline at end of file diff --git a/src/Plugin.Maui.KeyListener/KeyboardBehaviorTrigger.cs b/src/Plugin.Maui.KeyListener/KeyboardBehaviorTrigger.cs deleted file mode 100644 index 792ad91..0000000 --- a/src/Plugin.Maui.KeyListener/KeyboardBehaviorTrigger.cs +++ /dev/null @@ -1,54 +0,0 @@ -namespace Plugin.Maui.KeyListener; - -public sealed class KeyboardBehaviorTriggers : List { } - -public sealed partial class KeyboardBehaviorTrigger -{ - KeyboardModifiers _modifiers; - KeyboardKeys _keys; - - public KeyboardModifiers Modifiers - { - get => _modifiers; - - set - { - _modifiers = value; -#if IOS || MACCATALYST || WINDOWS - SetPlatformModifiers(_modifiers); -#endif - } - } - - public KeyboardKeys Keys - { - get => _keys; - - set - { - _keys = value; -#if IOS || MACCATALYST || WINDOWS - SetPlatformKeys(_keys); -#endif - } - } - - public override bool Equals(object obj) - { - if (obj is not KeyboardBehaviorTrigger trigger) - return false; - - return Keys == trigger.Keys && Modifiers == trigger.Modifiers; - } - - public override int GetHashCode() - { - unchecked - { - int hash = 17; - hash = hash * 23 + Keys.GetHashCode(); - hash = hash * 23 + Modifiers.GetHashCode(); - return hash; - } - } -} \ No newline at end of file diff --git a/src/Plugin.Maui.KeyListener/KeyboardBehaviorTrigger.iOS.cs b/src/Plugin.Maui.KeyListener/KeyboardBehaviorTrigger.iOS.cs deleted file mode 100644 index 8253937..0000000 --- a/src/Plugin.Maui.KeyListener/KeyboardBehaviorTrigger.iOS.cs +++ /dev/null @@ -1,41 +0,0 @@ -#if IOS || MACCATALYST -using UIKit; - -namespace Plugin.Maui.KeyListener; - -public sealed partial class KeyboardBehaviorTrigger -{ - internal UIKeyModifierFlags PlatformModifiers { get; private set; } - internal List PlatformKeys { get; private set; } - - void SetPlatformModifiers(KeyboardModifiers modifiers) - => PlatformModifiers = modifiers.ToPlatformModifiers(); - - void SetPlatformKeys(KeyboardKeys keys) - => PlatformKeys = keys.ToPlatformKeyValues(); -} - -internal class KeyboardBehaviorTriggerComparer : IEqualityComparer -{ - public bool Equals(KeyboardBehaviorTrigger x, KeyboardBehaviorTrigger y) - { - if (x == null && y == null) - return true; - if (x == null || y == null) - return false; - - return x.PlatformKeys.SequenceEqual(y.PlatformKeys) && x.PlatformModifiers == y.PlatformModifiers; - } - - public int GetHashCode(KeyboardBehaviorTrigger obj) - { - unchecked - { - int hash = 17; - hash = hash * 23 + obj.Keys.GetHashCode(); - hash = hash * 23 + obj.PlatformModifiers.GetHashCode(); - return hash; - } - } -} -#endif \ No newline at end of file diff --git a/src/Plugin.Maui.KeyListener/KeyboardFlags.cs b/src/Plugin.Maui.KeyListener/KeyboardFlags.cs index fc1696a..e2903b9 100644 --- a/src/Plugin.Maui.KeyListener/KeyboardFlags.cs +++ b/src/Plugin.Maui.KeyListener/KeyboardFlags.cs @@ -10,7 +10,6 @@ public enum KeyboardModifiers Alt = 16 } -[Flags] public enum KeyboardKeys { None, diff --git a/src/Plugin.Maui.KeyListener/KeyboardKeysExtensions.Windows.cs b/src/Plugin.Maui.KeyListener/KeyboardKeysExtensions.Windows.cs index a710d51..b4ab6f2 100644 --- a/src/Plugin.Maui.KeyListener/KeyboardKeysExtensions.Windows.cs +++ b/src/Plugin.Maui.KeyListener/KeyboardKeysExtensions.Windows.cs @@ -4,106 +4,9 @@ namespace Plugin.Maui.KeyListener; -internal static class KeyboardKeysExtensions +internal static partial class KeyboardKeysExtensions { - internal static VirtualKey ToPlatformKeys(this KeyboardKeys keyboardKeys) - { - List platformKeyValues = new(); - - foreach (KeyboardKeys keyboardKey in Enum.GetValues(typeof(KeyboardKeys))) - { - if (keyboardKeys.HasFlag(keyboardKey)) - { - VirtualKey platformKey = ToPlatformKey(keyboardKey); - - if (platformKey != 0) - platformKeyValues.Add(platformKey); - } - } - - var platformKeys = ToPlatformKeys(platformKeyValues); - - return platformKeys; - } - - internal static List ToPlatformKeyValues(this KeyboardKeys keyboardKeys) - { - List platformKeyValues = new(); - - foreach (KeyboardKeys keyboardKey in Enum.GetValues(typeof(KeyboardKeys))) - { - if (keyboardKeys.HasFlag(keyboardKey)) - { - VirtualKey platformKey = ToPlatformKey(keyboardKey); - - if (platformKey != 0) - platformKeyValues.Add(platformKey); - } - } - - return platformKeyValues; - } - internal static KeyboardKeys ToKeyboardKeys(this VirtualKey platformKeys) - { - List keyboardKeyValues = new(); - - foreach (VirtualKey platformKey in Enum.GetValues(typeof(VirtualKey))) - { - if (platformKeys.HasFlag(platformKey)) - { - KeyboardKeys keyboardKey = ToKeyboardKey(platformKey); - - if (keyboardKey != 0) - keyboardKeyValues.Add(keyboardKey); - } - } - - var keyboardKeys = ToKeyboardKeys(keyboardKeyValues); - - return keyboardKeys; - } - - internal static KeyboardKeys ToKeyboardKeys(this List platformKeys) - { - List keyboardKeyValues = new(); - - foreach (VirtualKey platformKey in platformKeys) - { - if (platformKeys.Contains(platformKey)) - { - KeyboardKeys keyboardKey = ToKeyboardKey(platformKey); - - if (keyboardKey != 0) - keyboardKeyValues.Add(keyboardKey); - } - } - - var keyboardKeys = ToKeyboardKeys(keyboardKeyValues); - - return keyboardKeys; - } - - internal static VirtualKey ToPlatformKeys(List platformKeyValues) - { - VirtualKey platformKeys = 0; - - foreach (VirtualKey platformKey in platformKeyValues) - platformKeys |= platformKey; - - return platformKeys; - } - - internal static KeyboardKeys ToKeyboardKeys(List keyboardKeyValues) - { - KeyboardKeys keyboardKeys = 0; - - foreach (KeyboardKeys keyboardKey in keyboardKeyValues) - keyboardKeys |= keyboardKey; - - return keyboardKeys; - } - - static VirtualKey ToPlatformKey(KeyboardKeys keyboardKey) => keyboardKey switch + internal static VirtualKey ToPlatformKey(KeyboardKeys keyboardKey) => keyboardKey switch { KeyboardKeys.A => VirtualKey.A, KeyboardKeys.B => VirtualKey.B, KeyboardKeys.C => VirtualKey.C, KeyboardKeys.D => VirtualKey.D, @@ -142,7 +45,7 @@ internal static KeyboardKeys ToKeyboardKeys(List keyboardKeyValues _ => 0 }; - static KeyboardKeys ToKeyboardKey(VirtualKey platformKey) => platformKey switch + internal static KeyboardKeys ToKeyboardKey(this VirtualKey platformKey) => platformKey switch { VirtualKey.A => KeyboardKeys.A, VirtualKey.B => KeyboardKeys.B, @@ -232,7 +135,7 @@ internal static KeyboardKeys ToKeyboardKeys(List keyboardKeyValues VirtualKey.NumberPad7 => KeyboardKeys.NumPad7, VirtualKey.NumberPad8 => KeyboardKeys.NumPad8, VirtualKey.NumberPad9 => KeyboardKeys.NumPad9, - _ => 0 + _ => KeyboardKeys.None }; } #endif \ No newline at end of file diff --git a/src/Plugin.Maui.KeyListener/KeyboardKeysExtensions.cs b/src/Plugin.Maui.KeyListener/KeyboardKeysExtensions.cs new file mode 100644 index 0000000..587c9a3 --- /dev/null +++ b/src/Plugin.Maui.KeyListener/KeyboardKeysExtensions.cs @@ -0,0 +1,114 @@ +namespace Plugin.Maui.KeyListener; +internal static partial class KeyboardKeysExtensions +{ + internal static char? ToChar(this KeyboardKeys key, KeyboardModifiers modifiers = KeyboardModifiers.None) => WithModifier(key switch + { + KeyboardKeys.A => 'a', + KeyboardKeys.B => 'b', + KeyboardKeys.Backquote => '`', + KeyboardKeys.Backslash => '\\', + KeyboardKeys.Backspace => (char)8, + KeyboardKeys.C => 'c', + KeyboardKeys.Comma => ',', + // CapsLock + KeyboardKeys.D => 'd', + KeyboardKeys.Delete => (char)127, + // DownArrow + KeyboardKeys.E => 'e', + // End + KeyboardKeys.Enter => (char)10, // Carriage Return + KeyboardKeys.Equals => '=', + KeyboardKeys.Escape => (char)27, + KeyboardKeys.F => 'f', + // F1 - F12 + KeyboardKeys.G => 'g', + KeyboardKeys.GraveAccent => (char)96, + KeyboardKeys.H => 'h', + // Home + KeyboardKeys.I => 'i', + // Insert + KeyboardKeys.J => 'j', + KeyboardKeys.K => 'k', + KeyboardKeys.L => 'l', + // LeftAlt + // LeftArrow + KeyboardKeys.LeftBracket => '[', + // LeftCommand + // LeftControl + // LeftShift + KeyboardKeys.M => 'm', + KeyboardKeys.Minus => '-', + KeyboardKeys.N => 'N', + // None + KeyboardKeys.Number0 => '0', + KeyboardKeys.Number1 => '1', + KeyboardKeys.Number2 => '2', + KeyboardKeys.Number3 => '3', + KeyboardKeys.Number4 => '4', + KeyboardKeys.Number5 => '5', + KeyboardKeys.Number6 => '6', + KeyboardKeys.Number7 => '7', + KeyboardKeys.Number8 => '8', + KeyboardKeys.Number9 => '9', + // NumLock + KeyboardKeys.NumPad0 => '0', + KeyboardKeys.NumPad1 => '1', + KeyboardKeys.NumPad2 => '2', + KeyboardKeys.NumPad3 => '3', + KeyboardKeys.NumPad4 => '4', + KeyboardKeys.NumPad5 => '5', + KeyboardKeys.NumPad6 => '6', + KeyboardKeys.NumPad7 => '7', + KeyboardKeys.NumPad8 => '8', + KeyboardKeys.NumPad9 => '9', + KeyboardKeys.NumPadDecimal => '.', + KeyboardKeys.NumPadDivide => '/', + KeyboardKeys.NumPadEnter => (char)10, + KeyboardKeys.NumPadMinus => '-', + KeyboardKeys.NumPadMultiply => '*', + KeyboardKeys.NumPadPeriod => '.', + KeyboardKeys.NumPadPlus => '+', + KeyboardKeys.O => 'o', + KeyboardKeys.P => 'p', + // PageUp + // PageDown + // Pause + KeyboardKeys.Period => '.', + KeyboardKeys.Plus => '+', + // PrintScreen + KeyboardKeys.Q => 'q', + KeyboardKeys.Quote => '"', + KeyboardKeys.R => 'r', + KeyboardKeys.Return => (char)10, + // RightAlt + // RightArrow + KeyboardKeys.RightBracket => ']', + // RightCommand + // RightCtrl + // RightShift + KeyboardKeys.S => 's', + // ScrollLock + KeyboardKeys.Semicolon => ';', + KeyboardKeys.Slash => '/', + KeyboardKeys.Space => ' ', + KeyboardKeys.T => 't', + KeyboardKeys.Tab => '\t', + KeyboardKeys.U => 'u', + // UpArrow + KeyboardKeys.V => 'v', + KeyboardKeys.W => 'w', + KeyboardKeys.X => 'x', + KeyboardKeys.Y => 'y', + KeyboardKeys.Z => 'z', + _ => (char?)null + }, modifiers); + + internal static char? WithModifier(this char? input, KeyboardModifiers modifiers = KeyboardModifiers.None) + { + if (!input.HasValue) { return null; } + + return modifiers.HasFlag(KeyboardModifiers.Shift) + ? char.ToUpper(input.Value) + : input; + } +} diff --git a/src/Plugin.Maui.KeyListener/KeyboardKeysExtensions.iOS.cs b/src/Plugin.Maui.KeyListener/KeyboardKeysExtensions.iOS.cs index e7a8408..9aa3061 100644 --- a/src/Plugin.Maui.KeyListener/KeyboardKeysExtensions.iOS.cs +++ b/src/Plugin.Maui.KeyListener/KeyboardKeysExtensions.iOS.cs @@ -3,7 +3,7 @@ namespace Plugin.Maui.KeyListener; -internal static class KeyboardKeysExtensions +internal static partial class KeyboardKeysExtensions { internal static UIKeyboardHidUsage ToPlatformKeys(this KeyboardKeys keyboardKeys) { @@ -103,7 +103,7 @@ internal static KeyboardKeys ToKeyboardKeys(List keyboardKeyValues return keyboardKeys; } - static UIKeyboardHidUsage ToPlatformKey(KeyboardKeys keyboardKey) => keyboardKey switch + internal static UIKeyboardHidUsage ToPlatformKey(this KeyboardKeys keyboardKey) => keyboardKey switch { KeyboardKeys.A => UIKeyboardHidUsage.KeyboardA, KeyboardKeys.B => UIKeyboardHidUsage.KeyboardB, @@ -186,7 +186,7 @@ internal static KeyboardKeys ToKeyboardKeys(List keyboardKeyValues _ => 0 }; - static KeyboardKeys ToKeyboardKey(UIKeyboardHidUsage platformKey) => platformKey switch + internal static KeyboardKeys ToKeyboardKey(this UIKeyboardHidUsage platformKey) => platformKey switch { UIKeyboardHidUsage.KeyboardA => KeyboardKeys.A, UIKeyboardHidUsage.KeyboardB => KeyboardKeys.B, diff --git a/src/Plugin.Maui.KeyListener/KeyboardPageViewController.cs b/src/Plugin.Maui.KeyListener/KeyboardPageViewController.cs index 864406d..504da9b 100644 --- a/src/Plugin.Maui.KeyListener/KeyboardPageViewController.cs +++ b/src/Plugin.Maui.KeyListener/KeyboardPageViewController.cs @@ -16,19 +16,19 @@ internal KeyboardPageViewController(IView page, IMauiContext mauiContext) public override void PressesBegan(NSSet presses, UIPressesEvent evt) { - ProcessPressses(UIPressesData.Create(presses, evt)); + ProcessPresses(UIPressesData.Create(presses, evt)); base.PressesBegan(presses, evt); } public override void PressesCancelled(NSSet presses, UIPressesEvent evt) { - ProcessPressses(UIPressesData.Create(presses, evt), true); + ProcessPresses(UIPressesData.Create(presses, evt), true); base.PressesCancelled(presses, evt); } public override void PressesEnded(NSSet presses, UIPressesEvent evt) { - ProcessPressses(UIPressesData.Create(presses, evt), true); + ProcessPresses(UIPressesData.Create(presses, evt), true); base.PressesEnded(presses, evt); } @@ -76,7 +76,7 @@ void CleanupTargets() _keyboardBehaviors.Remove(target); } - void ProcessPressses(UIPressesData presses, bool isKeyUp = false) + void ProcessPresses(UIPressesData presses, bool isKeyUp = false) { if (!_hasRegistrations) return; @@ -91,13 +91,13 @@ void ProcessPressses(UIPressesData presses, bool isKeyUp = false) var firstTarget = targets.First(); - char keyChar = presses.Keys.ToKeyboardKeys().ToString().ToCharArray().FirstOrDefault(); + var keyChar = presses.Keys.FirstOrDefault().ToKeyboardKey().ToChar(); var eventArgs = new KeyPressedEventArgs { Modifiers = presses.Modifiers.ToVirtualModifiers(), - Keys = presses.Keys.ToKeyboardKeys(), - KeyChar = keyChar + Key = presses.Keys.ToKeyboardKeys(), + KeyChar = keyChar ?? default }; if (isKeyUp) firstTarget.RaiseKeyUp(eventArgs);