@devries-cameron20 / t3code Mirror · read-only / commits / 511238c3bd
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 02:00 UTC

fix(mobile): Android terminal sends hardware-keyboard arrows, Tab, and Escape to the PTY (#32666)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Victor committed Nov 7, 2026 at 15:37 UTC 511238c3bd8388f26824f59c0ae1a4e70e0ee52d
3 files changed +97
apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/T3TerminalView.kt
+2
@@ -286,8 +286,10 @@ class T3TerminalView(context: Context, appContext: AppContext) : ExpoView(contex
286 onInput(mapOf("data" to "\r"))
287 },
288 )
289 + val navigationKeys = TerminalNavigationKeyListener { onInput(mapOf("data" to it)) }
290 inputView.setOnKeyListener { _, keyCode, event ->
291 if (readOnly) return@setOnKeyListener true
292 + if (navigationKeys.onKey(inputView, keyCode, event)) return@setOnKeyListener true
293 if (event.action != KeyEvent.ACTION_DOWN) return@setOnKeyListener false
294 when {
295 keyCode == KeyEvent.KEYCODE_DEL -> {
apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/TerminalNavigationKeyListener.kt new
+28
@@ -0,0 +1,28 @@
1 +package expo.modules.t3terminal
2 +
3 +import android.view.KeyEvent
4 +import android.view.View
5 +
6 +internal class TerminalNavigationKeyListener(
7 + private val send: (String) -> Unit
8 +) : View.OnKeyListener {
9 + override fun onKey(view: View?, keyCode: Int, event: KeyEvent): Boolean {
10 + val sequence = sequenceFor(keyCode, event) ?: return false
11 + if (event.action == KeyEvent.ACTION_DOWN) send(sequence)
12 + // Unconsumed arrow and Tab key events move focus off the terminal input.
13 + return true
14 + }
15 +
16 + // Matches iOS: these keys only without modifiers, plus Shift+Tab.
17 + private fun sequenceFor(keyCode: Int, event: KeyEvent): String? = when {
18 + keyCode == KeyEvent.KEYCODE_TAB && event.hasModifiers(KeyEvent.META_SHIFT_ON) -> "\u001b[Z"
19 + !event.hasNoModifiers() -> null
20 + keyCode == KeyEvent.KEYCODE_DPAD_UP -> "\u001b[A"
21 + keyCode == KeyEvent.KEYCODE_DPAD_DOWN -> "\u001b[B"
22 + keyCode == KeyEvent.KEYCODE_DPAD_RIGHT -> "\u001b[C"
23 + keyCode == KeyEvent.KEYCODE_DPAD_LEFT -> "\u001b[D"
24 + keyCode == KeyEvent.KEYCODE_TAB -> "\t"
25 + keyCode == KeyEvent.KEYCODE_ESCAPE -> "\u001b"
26 + else -> null
27 + }
28 +}
apps/mobile/modules/t3-terminal/android/src/test/java/expo/modules/t3terminal/TerminalNavigationKeyListenerTest.kt new
+67
@@ -0,0 +1,67 @@
1 +package expo.modules.t3terminal
2 +
3 +import android.app.Activity
4 +import android.view.KeyEvent
5 +import android.view.View
6 +import android.widget.Button
7 +import android.widget.EditText
8 +import android.widget.LinearLayout
9 +import org.junit.Assert.assertEquals
10 +import org.junit.Assert.assertTrue
11 +import org.junit.Test
12 +import org.junit.runner.RunWith
13 +import org.robolectric.Robolectric
14 +import org.robolectric.RobolectricTestRunner
15 +import org.robolectric.annotation.Config
16 +
17 +@RunWith(RobolectricTestRunner::class)
18 +@Config(sdk = [36], manifest = Config.NONE)
19 +class TerminalNavigationKeyListenerTest {
20 + @Test
21 + fun hardwareNavigationKeysReachThePtyAndKeepFocus() {
22 + val activity = Robolectric.buildActivity(Activity::class.java).setup().get()
23 + val sent = StringBuilder()
24 + val input = EditText(activity).apply {
25 + setSingleLine(true)
26 + isFocusableInTouchMode = true
27 + setOnKeyListener(TerminalNavigationKeyListener { sent.append(it) })
28 + }
29 + val nextControl = Button(activity).apply { isFocusableInTouchMode = true }
30 + val root = LinearLayout(activity).apply {
31 + orientation = LinearLayout.VERTICAL
32 + addView(input)
33 + addView(nextControl)
34 + }
35 + activity.setContentView(root)
36 + // Focus search below the input only finds controls that have bounds.
37 + root.measure(
38 + View.MeasureSpec.makeMeasureSpec(1080, View.MeasureSpec.EXACTLY),
39 + View.MeasureSpec.makeMeasureSpec(1920, View.MeasureSpec.EXACTLY),
40 + )
41 + root.layout(0, 0, 1080, 1920)
42 + assertTrue(input.requestFocus())
43 +
44 + val shift = KeyEvent.META_SHIFT_ON or KeyEvent.META_SHIFT_LEFT_ON
45 + val keys = listOf(
46 + Triple(KeyEvent.KEYCODE_DPAD_UP, 0, "\u001b[A"),
47 + Triple(KeyEvent.KEYCODE_DPAD_DOWN, 0, "\u001b[B"),
48 + Triple(KeyEvent.KEYCODE_DPAD_RIGHT, 0, "\u001b[C"),
49 + Triple(KeyEvent.KEYCODE_DPAD_LEFT, 0, "\u001b[D"),
50 + Triple(KeyEvent.KEYCODE_TAB, 0, "\t"),
51 + Triple(KeyEvent.KEYCODE_TAB, shift, "\u001b[Z"),
52 + Triple(KeyEvent.KEYCODE_ESCAPE, 0, "\u001b"),
53 + )
54 + for ((keyCode, meta, expected) in keys) {
55 + sent.clear()
56 + input.dispatchKeyEvent(KeyEvent(0, 0, KeyEvent.ACTION_DOWN, keyCode, 0, meta))
57 + input.dispatchKeyEvent(KeyEvent(0, 0, KeyEvent.ACTION_UP, keyCode, 0, meta))
58 + assertEquals("bytes for key $keyCode meta $meta", expected, sent.toString())
59 + assertTrue("focus left the input after key $keyCode meta $meta", input.isFocused)
60 + }
61 +
62 + sent.clear()
63 + val ctrl = KeyEvent.META_CTRL_ON or KeyEvent.META_CTRL_LEFT_ON
64 + input.dispatchKeyEvent(KeyEvent(0, 0, KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_UP, 0, ctrl))
65 + assertEquals("modified arrows are left to the app, as on iOS", "", sent.toString())
66 + }
67 +}