Skip to content

Commit 74091cf

Browse files
shivamhwpmaria-rcksjuliusmarmingeclaude
committed
fix(mobile): Android sends with Return on a hardware keyboard
On Android with a hardware keyboard, Return always inserted a new line and no key sent. The Android composer now handles hardware Return like iOS, with Ctrl in place of Command, and Settings > Keyboard shows on Android too. Fixes #21685 Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Julius Marminge <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 97c6773 commit 74091cf

11 files changed

Lines changed: 208 additions & 14 deletions

File tree

‎apps/mobile/modules/t3-composer-editor/android/src/main/java/expo/modules/t3composereditor/T3ComposerEditorModule.kt‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ internal object T3ComposerClipboard {
6666
}
6767

6868
class T3ComposerEditorModule : Module() {
69+
@Suppress("LongMethod") // A flat list of the view's props and events.
6970
override fun definition() = ModuleDefinition {
7071
Name("T3ComposerEditor")
7172

@@ -108,6 +109,12 @@ class T3ComposerEditorModule : Module() {
108109
Prop("readOnly") { view: T3ComposerEditorView, readOnly: Boolean ->
109110
view.setReadOnly(readOnly)
110111
}
112+
Prop("enterBehavior") { view: T3ComposerEditorView, behavior: String ->
113+
view.setEnterBehavior(behavior)
114+
}
115+
Prop("submitEnabled") { view: T3ComposerEditorView, enabled: Boolean ->
116+
view.setSubmitEnabled(enabled)
117+
}
111118
Prop("scrollEnabled") { view: T3ComposerEditorView, scrollEnabled: Boolean ->
112119
view.setScrollEnabled(scrollEnabled)
113120
}
@@ -132,6 +139,7 @@ class T3ComposerEditorModule : Module() {
132139
"onComposerSelectionChange",
133140
"onComposerFocus",
134141
"onComposerBlur",
142+
"onComposerSubmit",
135143
"onComposerPasteImages",
136144
"onComposerContextPress",
137145
"onComposerPasteContext",

‎apps/mobile/modules/t3-composer-editor/android/src/main/java/expo/modules/t3composereditor/T3ComposerEditorView.kt‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import android.util.TypedValue
2121
import android.view.DragEvent
2222
import android.view.Gravity
2323
import android.view.GestureDetector
24+
import android.view.InputDevice
2425
import android.view.MotionEvent
2526
import android.view.KeyEvent
2627
import 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)
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package expo.modules.t3composereditor
2+
3+
import android.text.InputType
4+
import android.view.InputDevice
5+
import android.view.KeyCharacterMap
6+
import android.view.KeyEvent
7+
import org.junit.Assert.assertEquals
8+
import org.junit.Test
9+
import org.junit.runner.RunWith
10+
import org.robolectric.RobolectricTestRunner
11+
import org.robolectric.RuntimeEnvironment
12+
import org.robolectric.Shadows.shadowOf
13+
import org.robolectric.annotation.Config
14+
import org.robolectric.shadows.InputDeviceBuilder
15+
16+
@RunWith(RobolectricTestRunner::class)
17+
@Config(sdk = [36], manifest = Config.NONE)
18+
class ComposerSubmitTest {
19+
private val sends = mutableListOf<Boolean>()
20+
private val editor = SelectionAwareEditText(RuntimeEnvironment.getApplication()).apply {
21+
inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_MULTI_LINE
22+
submitEnabled = true
23+
submitListener = { alternate -> sends += alternate }
24+
}
25+
26+
private fun keyboard(id: Int, type: Int = InputDevice.KEYBOARD_TYPE_ALPHABETIC): InputDevice =
27+
InputDeviceBuilder.newBuilder()
28+
.setId(id)
29+
.setSources(InputDevice.SOURCE_KEYBOARD)
30+
.setKeyboardType(type)
31+
.build()
32+
33+
private fun pressReturn(
34+
metaState: Int = 0,
35+
device: InputDevice = keyboard(1),
36+
repeatCount: Int = 0,
37+
flags: Int = 0
38+
) {
39+
val event = KeyEvent(
40+
0,
41+
0,
42+
KeyEvent.ACTION_DOWN,
43+
KeyEvent.KEYCODE_ENTER,
44+
repeatCount,
45+
metaState,
46+
device.id,
47+
0,
48+
flags,
49+
)
50+
// Robolectric's getDevice() reads the event's shadow, not the InputManager device list.
51+
shadowOf(event).setDevice(device)
52+
editor.onKeyDown(KeyEvent.KEYCODE_ENTER, event)
53+
}
54+
55+
@Test
56+
fun sendBehaviorSendsOnReturnAndInsertsANewlineOnShiftReturn() {
57+
pressReturn()
58+
pressReturn(KeyEvent.META_CTRL_ON or KeyEvent.META_CTRL_LEFT_ON)
59+
pressReturn(KeyEvent.META_SHIFT_ON or KeyEvent.META_SHIFT_LEFT_ON)
60+
61+
assertEquals(listOf(false, true), sends)
62+
assertEquals("\n", editor.text.toString())
63+
}
64+
65+
@Test
66+
fun newlineBehaviorSendsOnlyWithCtrl() {
67+
editor.returnSends = false
68+
69+
pressReturn()
70+
pressReturn(KeyEvent.META_CTRL_ON)
71+
pressReturn(KeyEvent.META_CTRL_ON or KeyEvent.META_SHIFT_ON)
72+
73+
assertEquals(listOf(false, true), sends)
74+
assertEquals("\n", editor.text.toString())
75+
}
76+
77+
@Test
78+
fun onScreenKeyboardReturnInsertsANewline() {
79+
pressReturn(
80+
device = keyboard(KeyCharacterMap.VIRTUAL_KEYBOARD),
81+
flags = KeyEvent.FLAG_SOFT_KEYBOARD
82+
)
83+
// Some IMEs send device 0, the phone's key-only built-in "keyboard", without FLAG_SOFT_KEYBOARD.
84+
pressReturn(device = keyboard(0, InputDevice.KEYBOARD_TYPE_NON_ALPHABETIC))
85+
86+
assertEquals(emptyList<Boolean>(), sends)
87+
assertEquals("\n\n", editor.text.toString())
88+
}
89+
90+
@Test
91+
fun heldReturnSendsOnce() {
92+
pressReturn()
93+
pressReturn(repeatCount = 1)
94+
pressReturn(repeatCount = 2)
95+
96+
assertEquals(listOf(false), sends)
97+
assertEquals("", editor.text.toString())
98+
}
99+
100+
@Test
101+
fun returnNeverSendsFromAComposerThatCannotSubmit() {
102+
editor.submitEnabled = false
103+
pressReturn()
104+
editor.submitEnabled = true
105+
106+
editor.readOnly = true
107+
pressReturn()
108+
109+
assertEquals(emptyList<Boolean>(), sends)
110+
}
111+
}

‎apps/mobile/src/features/settings/SettingsFollowUpRouteScreen.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView";
22
import { useAtomSet, useAtomValue } from "@effect/atom-react";
33
import { AsyncResult } from "effect/reactivity";
4+
import { Platform } from "react-native";
45
import { useSafeAreaInsets } from "react-native-safe-area-context";
56

67
import { AppText as Text } from "../../components/ui/AppText";
@@ -59,7 +60,7 @@ export function SettingsFollowUpRouteScreen() {
5960
</SettingsSection>
6061
<Text className="px-2 text-sm text-foreground-muted">
6162
Long-press the send button to use the other option for a single message. With a hardware
62-
keyboard, hold Command while sending.
63+
keyboard, hold {Platform.OS === "android" ? "Ctrl" : "Command"} while sending.
6364
</Text>
6465
</ScrollView>
6566
);

‎apps/mobile/src/features/settings/SettingsKeyboardRouteScreen.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView";
22
import { useAtomSet, useAtomValue } from "@effect/atom-react";
33
import { AsyncResult } from "effect/reactivity";
4+
import { Platform } from "react-native";
45
import { useSafeAreaInsets } from "react-native-safe-area-context";
56

67
import { AppText as Text } from "../../components/ui/AppText";
@@ -13,6 +14,8 @@ import { ListChoiceRow as SettingsChoiceRow } from "../../components/ui/ListChoi
1314
import { SettingsScreen } from "./components/SettingsScreen";
1415
import { SettingsSection } from "./components/SettingsSection";
1516

17+
const SEND_MODIFIER = Platform.OS === "android" ? "Ctrl" : "Command";
18+
1619
const ENTER_BEHAVIOR_OPTIONS: ReadonlyArray<{
1720
readonly behavior: ComposerEnterBehavior;
1821
readonly label: string;
@@ -26,7 +29,7 @@ const ENTER_BEHAVIOR_OPTIONS: ReadonlyArray<{
2629
{
2730
behavior: "newline",
2831
label: "Insert new line",
29-
description: "Return inserts a new line. Command-Return sends the message.",
32+
description: `Return inserts a new line. ${SEND_MODIFIER}-Return sends the message.`,
3033
},
3134
];
3235

‎apps/mobile/src/features/settings/SettingsRouteScreen.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ function useSettingsIndexSections(): SettingsListSection[] {
246246
...(navigationCustomizationAvailable()
247247
? [page("SettingsNavigation", "line.3.horizontal", "Navigation tabs")]
248248
: []),
249-
...(Platform.OS === "ios" ? [page("SettingsKeyboard", "keyboard", "Keyboard")] : []),
249+
page("SettingsKeyboard", "keyboard", "Keyboard"),
250250
page("SettingsVoice", "mic", "Voice"),
251251
],
252252
},

‎apps/mobile/src/native/T3ComposerEditor.native.tsx‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { PASTED_TEXT_ATTACHMENT_THRESHOLD_BYTES } from "@t3tools/client-runtime/
22
import { PROVIDER_SEND_TURN_MAX_INPUT_CHARS } from "@t3tools/contracts";
33
import { collectComposerInlineTokens } from "@t3tools/shared/composerInlineTokens";
44
import { composerContextEditorTokens, composerSkillLabels } from "../lib/composerContext";
5+
import { DEFAULT_COMPOSER_ENTER_BEHAVIOR } from "../lib/composerEnterBehavior";
56
import { requireNativeView } from "expo";
67
import { TextInputWrapper } from "expo-paste-input";
78
import {
@@ -89,6 +90,8 @@ interface NativeComposerEditorProps extends ViewProps {
8990
readonly autoFocus: boolean;
9091
readonly autoCorrect: boolean;
9192
readonly spellCheck: boolean;
93+
readonly enterBehavior: string;
94+
readonly submitEnabled: boolean;
9295
readonly onComposerChange: (event: NativeEditorEvent) => void;
9396
readonly onComposerSelectionChange?: (event: NativeSelectionEvent) => void;
9497
readonly onComposerPasteImages?: (event: NativePasteImagesEvent) => void;
@@ -106,6 +109,7 @@ interface NativeComposerEditorProps extends ViewProps {
106109
readonly onComposerContentSizeChange?: (
107110
event: NativeSyntheticEvent<{ readonly width: number; readonly height: number }>,
108111
) => void;
112+
readonly onComposerSubmit?: (event: NativeSyntheticEvent<{ alternate: boolean }>) => void;
109113
}
110114

111115
const NativeView = requireNativeView<NativeComposerEditorProps>(NATIVE_MODULE_NAME);
@@ -135,6 +139,7 @@ export function ComposerEditor({
135139
onFocus,
136140
onBlur,
137141
onContentSizeChange,
142+
onSubmit,
138143
contentInsetVertical = 0,
139144
...props
140145
}: ComposerEditorProps) {
@@ -318,6 +323,8 @@ export function ComposerEditor({
318323
autoFocus={props.autoFocus ?? false}
319324
autoCorrect={props.autoCorrect ?? true}
320325
spellCheck={props.spellCheck ?? true}
326+
enterBehavior={props.enterBehavior ?? DEFAULT_COMPOSER_ENTER_BEHAVIOR}
327+
submitEnabled={onSubmit !== undefined}
321328
textPasteThresholdBytes={onPasteText ? PASTED_TEXT_ATTACHMENT_THRESHOLD_BYTES : 0}
322329
maxInputChars={PROVIDER_SEND_TURN_MAX_INPUT_CHARS}
323330
style={{ flex: 1, minHeight: 0 }}
@@ -386,6 +393,11 @@ export function ComposerEditor({
386393
? undefined
387394
: (event) => onContentSizeChange(event.nativeEvent)
388395
}
396+
onComposerSubmit={
397+
onSubmit === undefined
398+
? undefined
399+
: (event) => onSubmit(event.nativeEvent.alternate === true)
400+
}
389401
/>
390402
</TextInputWrapper>
391403
);

‎apps/mobile/src/native/T3ComposerEditor.types.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,10 @@ export interface ComposerEditorProps {
8383
readonly width: number;
8484
readonly height: number;
8585
}) => void;
86-
/**
87-
* Hardware-keyboard Return behavior on iOS. No-op on Android, which has no
88-
* hardware Return handling.
89-
*/
86+
/** Hardware-keyboard Return behavior. Soft keyboards always insert a newline. */
9087
readonly enterBehavior?: ComposerEnterBehavior;
9188
readonly submitTitle?: string;
9289
readonly alternateSubmitTitle?: string;
93-
/** Command chords set alternate to reverse follow-up behavior; Android always passes false. */
90+
/** Command chords (Ctrl on Android) reverse the configured follow-up behavior. */
9491
readonly onSubmit?: (alternate: boolean) => void;
9592
}

‎apps/mobile/src/persistence/mobile-preferences.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export interface Preferences {
110110
readonly connectOnboardingOptOutAccounts?: ReadonlyArray<string>;
111111
readonly collapsedProjectGroups?: readonly string[];
112112
readonly dismissedDatabaseStorageNotices?: readonly string[];
113-
/** Hardware Return behavior is iOS-only. */
113+
/** Hardware-keyboard Return behavior in the composer. */
114114
readonly composerEnterBehavior?: ComposerEnterBehavior;
115115
readonly followUpBehavior?: FollowUpBehavior;
116116
readonly microphones?: ReadonlyArray<MicrophoneEntry>;

‎docs/user/composer.md‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,10 @@ start of the composer to edit the most recently queued message. Change
8585

8686
Mobile has the same choice under **Settings → Chat**. While a turn is
8787
running the send button shows which action it will take. Long-press it to use the
88-
other action for a single message, or hold `Cmd` while sending from a hardware
89-
keyboard. The button only offers Steer when the running agent supports it.
88+
other action for a single message. From a hardware keyboard, `Cmd+Return`
89+
(`Ctrl+Return` on Android) uses the other action, or `Cmd+Shift+Return`
90+
(`Ctrl+Shift+Return`) when Return inserts a new line. The button only offers
91+
Steer when the running agent supports it.
9092

9193
## Queue messages offline on mobile
9294

0 commit comments

Comments
 (0)