diff options
author | Marius Ungureanu <marius.ungureanu@xamarin.com> | 2022-02-24 05:08:48 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-24 05:08:48 +0300 |
commit | 4f3762af1ed9022a016c47a15f6bc7e9ec673c09 (patch) | |
tree | bf5db27979928f3778ec66170b39552212c3bac2 | |
parent | 4ce9da551fbd76b1115bfd3bc163f8010f9fc679 (diff) |
Fix up KeyboardUtil so it doesn't marshal strings unnecessarily multiple times.
-rw-r--r-- | Xwt.XamMac/Xwt.Mac/KeyboardUtil.cs | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/Xwt.XamMac/Xwt.Mac/KeyboardUtil.cs b/Xwt.XamMac/Xwt.Mac/KeyboardUtil.cs index ce93fe93..1da4587a 100644 --- a/Xwt.XamMac/Xwt.Mac/KeyboardUtil.cs +++ b/Xwt.XamMac/Xwt.Mac/KeyboardUtil.cs @@ -33,10 +33,14 @@ namespace Xwt.Mac { public static KeyEventArgs ToXwtKeyEventArgs (this NSEvent keyEvent) { + var characters = keyEvent.Characters; + var charactersIgnoringModifiers = keyEvent.CharactersIgnoringModifiers; + var keyCode = keyEvent.KeyCode; + NSEventModifierMask mask; - Key key = GetXwtKey(keyEvent, out mask); + Key key = GetXwtKey(keyEvent, keyCode, characters, charactersIgnoringModifiers, out mask); ModifierKeys mod = mask.ToXwtValue (); - return new KeyEventArgs (key, keyEvent.KeyCode, mod, keyEvent.IsARepeat, (long)TimeSpan.FromSeconds (keyEvent.Timestamp).TotalMilliseconds, keyEvent.Characters, keyEvent.CharactersIgnoringModifiers, keyEvent); + return new KeyEventArgs (key, keyCode, mod, keyEvent.IsARepeat, (long)TimeSpan.FromSeconds (keyEvent.Timestamp).TotalMilliseconds, characters, charactersIgnoringModifiers, keyEvent); } static Key RemoveShift(Key key, ref NSEventModifierMask mask) @@ -45,12 +49,12 @@ namespace Xwt.Mac return key; } - static Key GetXwtKey (NSEvent keyEvent, out NSEventModifierMask modMask) + static Key GetXwtKey (NSEvent keyEvent, ushort keyCode, string originalCharacters, string charactersIgnoringModifiers, out NSEventModifierMask modMask) { modMask = keyEvent.ModifierFlags; // special keys - switch (keyEvent.KeyCode) { + switch (keyCode) { case 65: return Key.NumPadDecimal; // kVK_ANSI_KeypadDecimal = 0x41 case 67: return Key.NumPadMultiply; // kVK_ANSI_KeypadMultiply = 0x43 case 69: return Key.NumPadAdd; // kVK_ANSI_KeypadPlus = 0x45 @@ -128,7 +132,7 @@ namespace Xwt.Mac // If you press the cmd+м on the keyboard, keyEvent.Characters contains a 'v' and keyEvent.CharactersIgnoringModifiers // contains the м // This way it can map cmd+м to paste like on a roman keyboard. - var characters = string.IsNullOrWhiteSpace(keyEvent.Characters) ? keyEvent.CharactersIgnoringModifiers : keyEvent.Characters; + var characters = string.IsNullOrWhiteSpace(originalCharacters) ? charactersIgnoringModifiers : originalCharacters; if (characters.Length > 0) switch (characters[0]) { case 'A': return Key.a; |