Skip to content

Commit a68cd86

Browse files
PR Batch Testerclaude
andcommitted
fix(mobile): only a physical keyboard's Return sends on Android
An IME can send Return with device ID 0 and no FLAG_SOFT_KEYBOARD, which the old check took for a hardware keyboard. Return now sends only when the event resolves to a real, non-virtual alphabetic keyboard. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 8a64055 commit a68cd86

2 files changed

Lines changed: 49 additions & 13 deletions

File tree

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import android.text.style.ReplacementSpan
1818
import android.util.TypedValue
1919
import android.view.Gravity
2020
import android.view.GestureDetector
21-
import android.view.KeyCharacterMap
21+
import android.view.InputDevice
2222
import android.view.MotionEvent
2323
import android.view.KeyEvent
2424
import android.view.ViewGroup
@@ -671,11 +671,12 @@ internal class SelectionAwareEditText(context: Context) : EditText(context) {
671671
* configured behavior and the Ctrl chord does the other one: with "send", Return sends,
672672
* Ctrl-Return sends the alternate way and Shift-Return inserts a newline; with "newline",
673673
* Ctrl-Return sends and Ctrl-Shift-Return sends the alternate way. Returns whether the send is
674-
* the alternate one, or null to insert a newline. Soft keyboards always insert a newline.
674+
* the alternate one, or null to insert a newline. Anything but a physical keyboard inserts a
675+
* newline.
675676
*/
676677
private fun submitChord(keyCode: Int, event: KeyEvent): Boolean? {
677678
val isReturn = keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_NUMPAD_ENTER
678-
if (!isReturn || isFromSoftKeyboard(event) || !canSubmit()) {
679+
if (!isReturn || !isFromPhysicalKeyboard(event) || !canSubmit()) {
679680
return null
680681
}
681682
return when {
@@ -687,9 +688,16 @@ internal class SelectionAwareEditText(context: Context) : EditText(context) {
687688
}
688689
}
689690

690-
private fun isFromSoftKeyboard(event: KeyEvent): Boolean =
691-
event.deviceId == KeyCharacterMap.VIRTUAL_KEYBOARD ||
692-
(event.flags and KeyEvent.FLAG_SOFT_KEYBOARD) != 0
691+
// An IME may send Return with any device ID and without FLAG_SOFT_KEYBOARD, so only a real
692+
// typing keyboard counts. Device ID 0 is no proof either: it aliases the built-in keyboard,
693+
// which on phones is a key-only device like the fingerprint sensor.
694+
private fun isFromPhysicalKeyboard(event: KeyEvent): Boolean {
695+
val device = event.device
696+
return (event.flags and KeyEvent.FLAG_SOFT_KEYBOARD) == 0 &&
697+
device != null &&
698+
!device.isVirtual &&
699+
device.keyboardType == InputDevice.KEYBOARD_TYPE_ALPHABETIC
700+
}
693701

694702
// No composing-text check, unlike iOS's marked-text guard: Latin IMEs keep a composing region
695703
// over the current word even for hardware typing, so it would block every send. An IME that

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

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package expo.modules.t3composereditor
22

33
import android.text.InputType
4+
import android.view.InputDevice
45
import android.view.KeyCharacterMap
56
import android.view.KeyEvent
67
import android.view.inputmethod.BaseInputConnection
@@ -9,7 +10,9 @@ import org.junit.Test
910
import org.junit.runner.RunWith
1011
import org.robolectric.RobolectricTestRunner
1112
import org.robolectric.RuntimeEnvironment
13+
import org.robolectric.Shadows.shadowOf
1214
import org.robolectric.annotation.Config
15+
import org.robolectric.shadows.InputDeviceBuilder
1316

1417
@RunWith(RobolectricTestRunner::class)
1518
@Config(sdk = [36], manifest = Config.NONE)
@@ -21,10 +24,20 @@ class ComposerSubmitTest {
2124
submitListener = { alternate -> sends += alternate }
2225
}
2326

27+
private val hardwareKeyboard = keyboard(1)
28+
29+
private fun keyboard(id: Int, type: Int = InputDevice.KEYBOARD_TYPE_ALPHABETIC): InputDevice =
30+
InputDeviceBuilder.newBuilder()
31+
.setId(id)
32+
.setSources(InputDevice.SOURCE_KEYBOARD)
33+
.setKeyboardType(type)
34+
.build()
35+
2436
private fun pressReturn(
2537
metaState: Int = 0,
26-
deviceId: Int = HARDWARE_KEYBOARD,
27-
repeatCount: Int = 0
38+
device: InputDevice? = hardwareKeyboard,
39+
repeatCount: Int = 0,
40+
flags: Int = 0
2841
) {
2942
val event = KeyEvent(
3043
0,
@@ -33,9 +46,12 @@ class ComposerSubmitTest {
3346
KeyEvent.KEYCODE_ENTER,
3447
repeatCount,
3548
metaState,
36-
deviceId,
49+
device?.id ?: UNKNOWN_DEVICE,
3750
0,
51+
flags,
3852
)
53+
// Robolectric's getDevice() reads the event's shadow, not the InputManager device list.
54+
shadowOf(event).setDevice(device)
3955
editor.onKeyDown(KeyEvent.KEYCODE_ENTER, event)
4056
}
4157

@@ -49,12 +65,24 @@ class ComposerSubmitTest {
4965
}
5066

5167
@Test
52-
fun shiftReturnAndSoftKeyboardReturnInsertNewlines() {
68+
fun shiftReturnInsertsANewline() {
5369
pressReturn(KeyEvent.META_SHIFT_ON or KeyEvent.META_SHIFT_LEFT_ON)
54-
pressReturn(deviceId = KeyCharacterMap.VIRTUAL_KEYBOARD)
5570

5671
assertEquals(emptyList<Boolean>(), sends)
57-
assertEquals("\n\n", editor.text.toString())
72+
assertEquals("\n", editor.text.toString())
73+
}
74+
75+
@Test
76+
fun returnFromAnythingButAPhysicalKeyboardInsertsANewline() {
77+
pressReturn(device = keyboard(KeyCharacterMap.VIRTUAL_KEYBOARD))
78+
// An IME can send device 0 without FLAG_SOFT_KEYBOARD. On phones that ID is the built-in
79+
// "keyboard", a key-only device such as the fingerprint sensor.
80+
pressReturn(device = keyboard(0, InputDevice.KEYBOARD_TYPE_NON_ALPHABETIC))
81+
pressReturn(device = null)
82+
pressReturn(flags = KeyEvent.FLAG_SOFT_KEYBOARD)
83+
84+
assertEquals(emptyList<Boolean>(), sends)
85+
assertEquals("\n\n\n\n", editor.text.toString())
5886
}
5987

6088
@Test
@@ -119,6 +147,6 @@ class ComposerSubmitTest {
119147
}
120148

121149
private companion object {
122-
const val HARDWARE_KEYBOARD = 1
150+
const val UNKNOWN_DEVICE = 99
123151
}
124152
}

0 commit comments

Comments
 (0)