From 1effa706fdeabb252c4154e8711441a4caf04210 Mon Sep 17 00:00:00 2001 From: Steve Bilogan Date: Fri, 5 Feb 2021 10:13:49 -0500 Subject: [PATCH] feat(TextBox): Add keyboard command capability to macOS TextBox --- .../Controls/TextBox/TextBoxView.macOS.cs | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBoxView.macOS.cs b/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBoxView.macOS.cs index 94537c5cee14..20523034d991 100644 --- a/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBoxView.macOS.cs +++ b/src/Uno.UI/UI/Xaml/Controls/TextBox/TextBoxView.macOS.cs @@ -25,7 +25,44 @@ public TextBoxView(TextBox textBox) Initialize(); } + public override bool PerformKeyEquivalent(NSEvent theEvent) + { + if (theEvent.Type == NSEventType.KeyDown) + { + if ((theEvent.ModifierFlags & NSEventModifierMask.DeviceIndependentModifierFlagsMask) == NSEventModifierMask.CommandKeyMask) + { + var selectorName = theEvent.CharactersIgnoringModifiers.ToLowerInvariant() switch + { + "x" => "cut:", + "c" => "copy:", + "v" => "paste:", + "z" => "undo:", + "a" => "selectAll:", + _ => string.Empty, + }; + + if (!string.IsNullOrWhiteSpace(selectorName)) + { + if (NSApplication.SharedApplication.SendAction(new ObjCRuntime.Selector(selectorName), null, this)) + { + return true; + } + } + } + else if ((theEvent.ModifierFlags & NSEventModifierMask.DeviceIndependentModifierFlagsMask) == (NSEventModifierMask.CommandKeyMask | NSEventModifierMask.ShiftKeyMask)) + { + if (theEvent.CharactersIgnoringModifiers.ToLowerInvariant() == "z") + { + if (NSApplication.SharedApplication.SendAction(new ObjCRuntime.Selector("redo:"), null, this)) + { + return true; + } + } + } + } + return base.PerformKeyEquivalent(theEvent); + } private void OnEditingChanged(object sender, EventArgs e) { OnTextChanged();