Skip to content

feat(mobile): capture hardware-keyboard keys in the terminal - #4666

Closed
smk508 wants to merge 1 commit into
stablyai:mainfrom
smk508:feat/mobile-hardware-keyboard-capture
Closed

smk508 wants to merge 1 commit into
stablyai:mainfrom
smk508:feat/mobile-hardware-keyboard-capture

Conversation

@smk508

@smk508 smk508 commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

What & why

On an iPad with an attached hardware keyboard, the mobile terminal can only send printable text. A physical Ctrl, Alt, arrow, Esc, Tab, or function key does nothing — so you can't Ctrl-C a process, navigate shell history, or drive a TUI without tapping the on-screen accessory bar (which forces you to stop typing).

Root cause: keystrokes are forwarded through a hidden TextInput capture field, and iOS only delivers two things from it — printable characters (onChangeText) and Backspace (onKeyPress, via getTerminalLiveSpecialKeyBytes). Modifier combos and special keys never produce a usable JS event, so they're dropped.

This PR captures hardware key events at the native layer and routes them into the existing terminal-send pipeline.

How it works

The encoding was already solved — buildTerminalShortcutKey() in terminal-accessory-keys.ts turns any {key, modifiers} into the correct control bytes (Ctrl→control char, arrows→CSI, F1–F4→SS3, etc.), and the accessory-bar buttons already use it. The only missing piece was a source of hardware key events.

native key press ──▶ OrcaKeyCaptureView (Swift / Kotlin)
                       emits { keyCode|key, characters, ctrl/alt/shift/meta }
                          │
                          ▼
   nativeKeyEventToBytes()  ── reuses ──▶  buildTerminalShortcutKey()
                          │                         (unchanged, audited)
                          ▼
   sendLiveTerminalInput() ──▶ terminal.send  (unchanged)
  • New package @orca/expo-hardware-keyboard (mirrors expo-two-way-audio): a native ExpoView that becomes first responder when active and forwards raw presses.
    • iOS: overrides pressesBegan (UIKey: keyCode, characters, modifierFlags). Not a text input, so the software keyboard stays hidden while focused — correct for a hardware-keyboard iPad.
    • Android: focusable view overriding dispatchKeyEvent; resolves special keys + base char natively.
  • terminal-hardware-key.ts maps a native event → {key, modifiers} (iOS HID keycodes resolved in JS; Android pre-resolved) and calls the existing encoder. Unit-tested.
  • Session screen: a new "physical keyboard" toggle (⌨ in the accessory bar) adds a third input mode beside buffered and live. Enabling it mounts the capture view; it's mutually exclusive with live input and resets on disconnect.

No existing input path changes — this is purely additive.

Verification

  • ✅ terminal-hardware-key.test.ts — covers Ctrl+C, Ctrl+A, Alt+b, all arrows, Ctrl+Arrow, Esc, Tab, Shift+Tab, Backspace, Enter, F5, PageUp, Home, plus the Android pre-resolved path. The byte expectations were checked against the real buildTerminalShortcutKey.
  • ⚠️ Native paths are not yet exercised on-device — hence draft. The module needs a dev-client rebuild to run (it can't hot-reload). On-device checklist below.

Build & confirm on iPad (dev client)

# desktop RPC (port 6768) + Metro (8081) can both run on the dev box
pnpm install && pnpm dev            # repo root
cd mobile && pnpm install
pnpm exec expo run:ios --device     # build+sign+install the dev client on the iPad
pnpm start --dev-client             # serve JS; pair via Settings → Mobile

On-device checklist:

  • ⌨ toggle enables capture; status hint shows "Physical keyboard sends directly to terminal".
  • Letters/Enter type; Ctrl-C interrupts; arrows recall/move; Esc, Tab, Ctrl-R work in a shell and a TUI (e.g. htop, vim).
  • Toggling live-input off/disconnecting clears capture; software-keyboard users (no toggle) are unaffected.

Known limitations (follow-ups)

  • No key auto-repeat yet (holding an arrow sends one event).
  • Shifted-punctuation + modifier combos rely on the existing applyShift table; worth a spot-check on device.
  • Toggle is always shown; could auto-surface only when isHardwareKeyboardConnected() (already exported by the module).

🤖 Generated with Claude Code

RN's TextInput only surfaces printable characters and Backspace on iOS, so a
physical Ctrl, Alt, arrow, Esc, Tab, or function key produced no JS event and
never reached the PTY — a hardware keyboard on iPad could only type text.

Add an expo-hardware-keyboard native module (iOS Swift + Android Kotlin) that
captures raw hardware key presses and forwards {key, modifiers} to JS, where the
existing buildTerminalShortcutKey() encodes the control bytes and the existing
sendLiveTerminalInput() delivers them. Wire a "physical keyboard" toggle into
the terminal session screen as a third input mode alongside buffered and live.

The byte-encoding reuses the audited terminal-accessory-keys logic; only the
native event source is new. terminal-hardware-key.ts is covered by unit tests.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@smk508

smk508 commented Jun 6, 2026

Copy link
Copy Markdown
Contributor Author

Withdrawing until verified on-device — the native key-capture paths haven't been built and tested on a real iPad yet. Branch feat/mobile-hardware-keyboard-capture (fork smk508/orca) is retained; will reopen once confirmed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants