GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(mobile): Android terminal sends hardware-keyboard arrows, Tab, an… · adampeterhiggins/t3code@511238c

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/mobile/modules/t3-terminal/android/src
    • main/java/expo/modules/t3terminal
      • T3TerminalView.kt
      • TerminalNavigationKeyListener.kt
    • test/java/expo/modules/t3terminal
      • TerminalNavigationKeyListenerTest.kt
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/T3TerminalView.kt‎

Copy file name to clipboardExpand all lines: apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/T3TerminalView.kt
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,10 @@ class T3TerminalView(context: Context, appContext: AppContext) : ExpoView(contex
286286
onInput(mapOf("data" to "\r"))
287287
},
288288
)
289+
val navigationKeys = TerminalNavigationKeyListener { onInput(mapOf("data" to it)) }
289290
inputView.setOnKeyListener { _, keyCode, event ->
290291
if (readOnly) return@setOnKeyListener true
292+
if (navigationKeys.onKey(inputView, keyCode, event)) return@setOnKeyListener true
291293
if (event.action != KeyEvent.ACTION_DOWN) return@setOnKeyListener false
292294
when {
293295
keyCode == KeyEvent.KEYCODE_DEL -> {
Collapse file

‎apps/mobile/modules/t3-terminal/android/src/main/java/expo/modules/t3terminal/TerminalNavigationKeyListener.kt‎

Copy file name to clipboard
+28Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}
Collapse file

‎apps/mobile/modules/t3-terminal/android/src/test/java/expo/modules/t3terminal/TerminalNavigationKeyListenerTest.kt‎

Copy file name to clipboard
+67Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.