@@ -21,6 +21,7 @@ import android.util.TypedValue
2121import android.view.DragEvent
2222import android.view.Gravity
2323import android.view.GestureDetector
24+ import android.view.InputDevice
2425import android.view.MotionEvent
2526import android.view.KeyEvent
2627import android.view.View
@@ -58,6 +59,7 @@ class T3ComposerEditorView(context: Context, appContext: AppContext) : ExpoView(
5859 private val onComposerSelectionChange by EventDispatcher ()
5960 private val onComposerFocus by EventDispatcher ()
6061 private val onComposerBlur by EventDispatcher ()
62+ private val onComposerSubmit by EventDispatcher ()
6163 private val onComposerPasteImages by EventDispatcher ()
6264 private val onComposerContextPress by EventDispatcher ()
6365 private val onComposerPasteContext by EventDispatcher ()
@@ -169,6 +171,9 @@ class T3ComposerEditorView(context: Context, appContext: AppContext) : ExpoView(
169171 ),
170172 )
171173 }
174+ editor.submitListener = { alternate ->
175+ onComposerSubmit(mapOf (" alternate" to alternate))
176+ }
172177 editor.setOnFocusChangeListener { _, hasFocus ->
173178 if (hasFocus) {
174179 onComposerFocus(emptyMap<String , Any >())
@@ -326,6 +331,14 @@ class T3ComposerEditorView(context: Context, appContext: AppContext) : ExpoView(
326331 editor.isCursorVisible = editor.isEnabled && ! readOnly
327332 }
328333
334+ fun setEnterBehavior (behavior : String ) {
335+ editor.returnSends = behavior != " newline"
336+ }
337+
338+ fun setSubmitEnabled (enabled : Boolean ) {
339+ editor.submitEnabled = enabled
340+ }
341+
329342 fun setScrollEnabled (scrollEnabled : Boolean ) {
330343 editor.isVerticalScrollBarEnabled = scrollEnabled
331344 }
@@ -617,6 +630,9 @@ internal class SelectionAwareEditText(
617630 var pasteImagesListener: ((List <String >) -> Unit )? = null
618631 var pasteContextListener: ((Map <String , String >) -> Unit )? = null
619632 var pasteTextListener: ((String , Int , Int ) -> Unit )? = null
633+ var submitListener: ((Boolean ) -> Unit )? = null
634+ var submitEnabled = false
635+ var returnSends = true
620636 var textPasteThresholdBytes = 0
621637 var maxInputChars = Int .MAX_VALUE
622638 var clipboardFragment = " "
@@ -674,7 +690,45 @@ internal class SelectionAwareEditText(
674690 return spans.isNotEmpty()
675691 }
676692
693+ // Mirrors the iOS key commands with Ctrl for Command. Returns `alternate`, or null to type Return.
694+ // No composing-text guard: some IMEs keep a composing region over the last word while hardware typing.
695+ private fun returnSubmission (keyCode : Int , event : KeyEvent ): Boolean? {
696+ val isReturn = keyCode == KeyEvent .KEYCODE_ENTER || keyCode == KeyEvent .KEYCODE_NUMPAD_ENTER
697+ if (! isReturn || ! canSubmit() || ! isFromPhysicalKeyboard(event)) return null
698+ return if (returnSends) {
699+ when {
700+ event.hasNoModifiers() -> false
701+ event.hasModifiers(KeyEvent .META_CTRL_ON ) -> true
702+ else -> null
703+ }
704+ } else {
705+ when {
706+ event.hasModifiers(KeyEvent .META_CTRL_ON ) -> false
707+ event.hasModifiers(KeyEvent .META_CTRL_ON or KeyEvent .META_SHIFT_ON ) -> true
708+ else -> null
709+ }
710+ }
711+ }
712+
713+ private fun canSubmit (): Boolean =
714+ submitEnabled && submitListener != null && ! readOnly && isEnabled
715+
716+ // IMEs can send Return from device 0 without FLAG_SOFT_KEYBOARD, so only a real alphabetic keyboard counts.
717+ private fun isFromPhysicalKeyboard (event : KeyEvent ): Boolean {
718+ val device = event.device
719+ return (event.flags and KeyEvent .FLAG_SOFT_KEYBOARD ) == 0 &&
720+ device != null &&
721+ ! device.isVirtual &&
722+ device.keyboardType == InputDevice .KEYBOARD_TYPE_ALPHABETIC
723+ }
724+
677725 override fun onKeyDown (keyCode : Int , event : KeyEvent ): Boolean {
726+ val alternate = returnSubmission(keyCode, event)
727+ if (alternate != null ) {
728+ // A held Return repeats; send once and swallow the repeats instead of typing newlines.
729+ if (event.repeatCount == 0 ) submitListener?.invoke(alternate)
730+ return true
731+ }
678732 val handled = when (keyCode) {
679733 KeyEvent .KEYCODE_DEL -> deleteChip(true )
680734 KeyEvent .KEYCODE_FORWARD_DEL -> deleteChip(false )
0 commit comments