From 2c187cc62d6ffb8a82b4af99025a3224eba3067a Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Tue, 16 Jan 2024 14:26:27 -0600 Subject: [PATCH] Process the enter key as "Done" --- .../Handlers/Entry/EntryHandler.Android.cs | 30 +++++++++++++++++-- .../Handlers/Entry/EntryHandler.Windows.cs | 4 --- .../Platform/Android/EditTextExtensions.cs | 9 +++++- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/Core/src/Handlers/Entry/EntryHandler.Android.cs b/src/Core/src/Handlers/Entry/EntryHandler.Android.cs index 72f8379807dd..636f4d2532a4 100644 --- a/src/Core/src/Handlers/Entry/EntryHandler.Android.cs +++ b/src/Core/src/Handlers/Entry/EntryHandler.Android.cs @@ -2,6 +2,7 @@ using Android.Graphics.Drawables; using Android.Text; using Android.Views; +using Android.Views.InputMethods; using AndroidX.AppCompat.Widget; using AndroidX.Core.Content; using static Android.Views.View; @@ -193,17 +194,40 @@ void OnEditorAction(object? sender, EditorActionEventArgs e) { var returnType = VirtualView?.ReturnType; + // Inside of the android implementations that map events to listeners, the default return value for "Handled" is always true + // This means, just by subscribing to EditorAction/KeyPressed/etc.. you change the behavior of the control + // So, we are setting handled to false here in order to maintain default behavior + bool handled = false; if (returnType != null) { - var currentInputImeFlag = returnType.Value.ToPlatform(); + var actionId = e.ActionId; + var evt = e.Event; + ImeAction currentInputImeFlag = PlatformView.ImeOptions; - if (e.IsCompletedAction(currentInputImeFlag)) + // On API 34 it looks like they fixed the issue where the actionId is ImeAction.ImeNull when using a keyboard + // so I'm just setting the actionId here to the current ImeOptions so the logic can all be simplified + if (actionId == ImeAction.ImeNull && evt?.KeyCode == Keycode.Enter) + { + actionId = currentInputImeFlag; + } + + // keyboard path + if (evt?.KeyCode == Keycode.Enter && evt?.Action == KeyEventActions.Down) + { + handled = true; + } + else if (evt?.KeyCode == Keycode.Enter && evt?.Action == KeyEventActions.Up) + { + VirtualView?.Completed(); + } + // InputPaneView Path + else if(evt?.KeyCode is null && (actionId == ImeAction.Done || actionId == currentInputImeFlag)) { VirtualView?.Completed(); } } - e.Handled = false; + e.Handled = handled; } private void OnSelectionChanged(object? sender, EventArgs e) diff --git a/src/Core/src/Handlers/Entry/EntryHandler.Windows.cs b/src/Core/src/Handlers/Entry/EntryHandler.Windows.cs index f01fd8e095c5..6b7ec0c2d829 100644 --- a/src/Core/src/Handlers/Entry/EntryHandler.Windows.cs +++ b/src/Core/src/Handlers/Entry/EntryHandler.Windows.cs @@ -123,10 +123,6 @@ void OnPlatformKeyUp(object? sender, KeyRoutedEventArgs args) { PlatformView?.TryMoveFocus(FocusNavigationDirection.Next); } - else - { - // TODO: Hide the soft keyboard; this matches the behavior of .NET MAUI on Android/iOS - } VirtualView?.Completed(); } diff --git a/src/Core/src/Platform/Android/EditTextExtensions.cs b/src/Core/src/Platform/Android/EditTextExtensions.cs index 50680d5b9950..c344b60d1081 100644 --- a/src/Core/src/Platform/Android/EditTextExtensions.cs +++ b/src/Core/src/Platform/Android/EditTextExtensions.cs @@ -320,11 +320,18 @@ internal static void SetInputType(this EditText editText, ITextInput textInput) editText.SetSelection(previousCursorPosition); } - internal static bool IsCompletedAction(this EditorActionEventArgs e, ImeAction? currentInputImeFlag) + internal static bool IsCompletedAction(this EditorActionEventArgs e, ImeAction currentInputImeFlag) { var actionId = e.ActionId; var evt = e.Event; + // On API 34 it looks like they fixed the issue where the actionId is ImeAction.ImeNull when using a keyboard + // so I'm just setting the actionId here to whatever the user has + if (actionId == ImeAction.ImeNull && evt?.KeyCode == Keycode.Enter) + { + actionId = currentInputImeFlag; + } + return actionId == ImeAction.Done || actionId == currentInputImeFlag ||