# Keyboard conversion prerequisite `src/keyboard.go` supplies an isolated, bidirectional conversion between XKB keysym names and NVDA Remote `vk_code`/`extended` identities. The subsequent `src/keyboard_state.go` adds outgoing key-pair planning with bounded state and write acknowledgements. A further reader increment adds typed `remote.key` IPC and an explicitly attached mailbox, with the external conversion bridge in `src/keyboard_bridge.go`. When the controlled user enables keyboard control for the session, the command requests `keyboard-inject` and the coordinator forwards converted relay keys through `remote.key`; the reader attaches its production input owner for that grant. Unconvertible keys are ignored for press and release; a full 256-key queue stops the connection. ## Mapping contract Construct an immutable map with an explicit profile: - `portable`: lowercase Latin letter key names, top-row digits, F1–F24, left/right Shift, Control, Alt and Super, Menu, Caps Lock, Num Lock, Scroll Lock, Pause, Print, Backspace, Tab, Escape, Space, Return, and keypad and navigation keys. `Prior`/`Next` and their keypad forms are accepted aliases. It also includes the eighteen browser, volume, media and launch keys, VK 0xA6–0xB7 (`XF86Back` through `XF86Calculator`; see `mediaKeys` in `src/keyboard.go`). These are layout independent and sent extended, as Windows does. - `us`: the same keys plus the eleven US OEM punctuation key identities. This requires an explicit US-layout agreement; it is never an automatic fallback. OEM meanings on other layouts remain unsupported. Input names must identify the unshifted key under the agreed layout, rather than the resulting text. `a` names the letter key; `A`, `exclam`, `ISO_Left_Tab`, dead keys and Unicode text are not normalized into guessed keys. A future Xorg owner must resolve the active layout and modifier/lock state. `portable` means a limited set of logical identities, not identical text on arbitrary layouts. Main Return and keypad Enter use different extended flags. Navigation keys, Insert and Delete also retain their main/keypad distinction, including keypad digits versus keypad navigation under Num Lock. Left/right modifier VKs are emitted explicitly; right Shift is **not** an extended key. Scan codes are never fabricated from X keycodes or keysyms. Incoming generic Control/Alt use the extended flag for side selection. Generic Shift uses scan `0x2a` (left) or `0x36` (right); an absent or zero scan code, as sent by Linux controllers derived from Orca Remote, means left Shift, and other scan codes are rejected. Side-specific VKs determine identity directly; other scan information is range-checked but does not override the identity or become an X keycode. Print Screen, the Windows keys, Menu, keypad divide and Num Lock, and the media keys, are always extended on Windows but are accepted without the flag, which those controllers omit. Other unsupported extended/VK combinations are rejected. The same conversion applies to both press and release, and outgoing press state survives the JSON codec. AltGr (`ISO_Level3_Shift`), level-five modifiers, IME/dead keys, other OEM profiles, OEM102, other XF86 keys (such as power and eject) and unsupported VKs remain unavailable. In particular, no implicit Control press is generated to emulate Windows AltGr. Unknown profiles, key names and wire identities return one constant error with no input text or partial key. The maps retain no caller input, held-key state, desktop handles or consent and can be read concurrently. ## Outgoing key-pair planning `keyboardEncoder` belongs to one nonzero capture epoch and one serial owner. It accepts source Xorg keycodes 8–255 and retains at most 32 distinct held keys. It stores wire identities and source slots, without retaining key names or mapper references. A fresh down uses the supplied explicit map; repeat and release retain that down's identity even if the name, layout/profile or Num Lock state changes. Fresh downs after a completed release use the current map. Events must come from a future trusted core capture service. Marked repeats are repeated downs of an already held source. Ordinary keys emit another down; modifier and lock-key repeats emit nothing. A release for an unowned source is suppressed. A second unmarked down for a held source, orphan repeat, repeat marked on an up, invalid source code, unsupported fresh mapping, colliding wire identity or held-key overflow stops admission and preserves existing ownership for cleanup. Two source slots cannot independently hold the same VK/extended identity; main and keypad Insert remain distinct. This detects codec identity collisions, not physical/injected collisions on the destination. Each emitted plan has a strictly increasing local sequence. That sequence is not added to the NVDA frame. The serial writer acknowledges the exact epoch and sequence only after its write succeeds. There can be only one outstanding plan; another `apply` returns backpressure without consuming the event or changing state. The caller must retain that event in its own bounded queue or stop the stream, rather than dropping it. Zero, duplicate, late, wrong-epoch and wrong-sequence acknowledgements cannot consume a pending plan. Sequence exhaustion stops admission instead of wrapping. A down reserves ownership before returning its plan. An up retains ownership until acknowledgement, so queue/write failure cannot erase the key from the cleanup ledger. Successful write acknowledgement is **not** proof that the relay or destination applied the event. On any uncertain write or admission failure, retire the original encoder. Retirement stops it permanently and transfers a bounded release plan to the caller in reverse down order, including unacknowledged downs and ups. It clears local state, so repeated retirement emits no duplicate plan; the caller owns retaining and delivering the returned plan. Stale epoch cleanup changes nothing. Fresh capture requires a new encoder and separate reader authorization; it cannot resume this instance. Neither the mapper nor encoder performs network, native or desktop I/O. The encoder is not safe for concurrent mutation; its future owner must serialize events, acknowledgements and retirement. It has no automatic retry, consent, capture acquisition or native recovery mechanism. ## Controller capture With a controller `keyboard-capture` lease, `remote.input` events from the reader's exclusive capture feed `key_output`, which uses this encoder and writes frames to the relay, acknowledging each after its write. Presses the agreed profile cannot represent are ignored with their repeats and release, so the encoder never stops on them. `remote-focus` sends `remote.focus` through the shared operation sequence. See [the reader capture contract](../../cthulhu/docs/tentacle-remote-capture.md). ## Incoming reader IPC `reader_key` converts an understood relay key through the selected immutable map to the public SDK's canonical `RemoteKey` identity and press state. Unknown, ambiguous and unsupported payloads return a constant error and no partial key. It does not add origin metadata, queue work or open a lease. Every canonical portable/US mapper result is checked against the SDK representation in tests. The reader's [typed keyboard contract](../../cthulhu/docs/tentacle-remote-keyboard.md) now supplies `RemoteKeyRequest` and `remote.key`. The host checks the original process, controlled role, `keyboard-inject` consent, deadline and shared operation sequence. An explicitly attached reader owner receives a bounded 256-item, 256-KiB mailbox; admission includes in-flight accounting until idempotent release. Overflow revokes the lease and drains pending input. Revoke, close, replacement, expiry, policy cancellation, disable and host failure invalidate accepted work. Stale owner releases cannot affect replacement capacity. No production reader owner calls `EnableRemoteKeys`, so ordinary sessions return owner-unavailable. The bridge is not attached to the live coordinator, and a successful test mailbox admission is not native injection completion. Captured key events, native key ownership, input recovery and live acceptance remain unfinished. Both SDK process fixtures exercise the mailbox with synthetic keys; they never inject into a desktop. ## Integration boundaries The reader now has a [quarantined destination-key prerequisite](../../cthulhu/docs/testing/remote-key-target.md). Its bounded ledger reserves native codes before down, uses the original code for up and retains uncertain effects/failed releases for retry. A private Xorg backend resolves fresh group-zero XKB snapshots and rejects unavailable or ambiguous mappings and lock-key downs. Native tests inspect owned key state across layout and Num Lock changes and preserve preexisting synthetic keys. The [source-built helper](../../cthulhu/docs/testing/remote-input-helper.md) now provides private process-lifetime, EOF and watchdog cleanup with bounded ACK handling. A quarantined reader-side Supervisor now owns launch, serial heartbeats and cancellation, requiring clean original-epoch retirement and process exit before replacement. Unconfirmed cleanup permanently blocks its replacement path. There is no production reader launcher or mailbox attachment. An explicit quarantined reader worker now joins the mailbox and Supervisor, rechecking original-lease authority and supplied policy before/after helper startup. It retains in-flight budget through completion/retirement, handles idle revoke and blocks replacement until successful cleanup/process exit. Failed cleanup ends its session and retains the Supervisor's uncertainty latch. Private SDK/helper-process and five Xvfb cases exercise this path; see [the worker contract](../../cthulhu/docs/testing/remote-input-helper.md#quarantined-leasemailbox-worker). This worker has no production caller or implemented physical-input policy. Two additional private native cases now kill/stop the process hosting the real SDK/host/mailbox worker and Supervisor. The independently grouped source helper releases held modifiers on descriptor EOF or watchdog expiry. Resuming the stopped owner leaves its mailbox disabled and replacement blocked after the unconfirmed helper exit. The outer fixture adopts/reaps orphaned children using Linux subreaper support; it does not depend on an init system. See [the process-failure evidence and limits](../../cthulhu/docs/testing/remote-input-helper.md#mailbox-owner-process-failure). Later physical same-code collisions, native repeat policy, production-reader crash recovery and input provenance still block production use. Failed native cleanup must block replacement input; an old release cannot be sent into a replacement helper. Private helper tests use separate writer and helper process groups, exclusive control/lifetime write ends and a separate X connection to inspect owned keys. Writer death/hang, explicit close, graceful signals, wrong epochs, replay, heartbeat renewal and stalled stdout retire the source-built helper without claiming a physical-key or blocked-X-server guarantee. Failed/missing cleanup confirmation blocks replacement; helper death itself still can leave keys held. No helper is launched by the source Remote command or ordinary reader startup. These helpers have no callers in the live coordinator. Before connecting them to production keyboard services, the reader must provide exclusive capture, trusted routing, delivery-time lease checks, a core escape and independent injection cleanup. The encoder supplies original wire key-pair identity, but the reader still must own actual capture/injection and the destination key ledger. Mapping failure must suspend input and release only owned keys. The caller must stop and join the old writer before delivering a retirement plan; never send old cleanup through a replacement connection or forget a failed release. A dead transport cannot deliver cleanup, so independent target cleanup remains necessary. Lock-state synchronization, physical collisions and bypass handling remain unfinished. The encoder requires normalized autorepeat: a repeated down with no synthetic release between repeats. Classic X11 release/press pairs must be normalized by the future native capture owner. The current reader XRecord `RepeatTracker` marks repeat downs but still exposes releases; it cannot be wired directly to this encoder. The reader's quarantined `RemoteRepeatNormalizer` now supplies that normalization: it holds a release briefly, folds a same-timestamp press into a repeat and releases owned keys on retirement. A private Xvfb check observed only detectable-style repeats through XRecord; classic pairs are unit-tested. See [the evidence](../../cthulhu/docs/testing/remote-input-feasibility.md#capture-side-repeat-normalization-2026-11-06). It has no production caller. A quarantined `RemoteCaptureRouter` then decides forwarding: ordinary keys and reader-modifier chords go remote, while a core escape and binding-derived local chords stay local, with forwarded prefixes released remotely first and local releases swallowed. See [the routing evidence](../../cthulhu/docs/testing/remote-input-feasibility.md#capture-routing-and-bypass-balancing-2026-11-06). Capture entry with already-held keys, source overflow, bypass chords and device/layout changes need explicit native policy and tests. Mapping a supported wire key does not establish that the destination's active keymap contains it. Injection must resolve destination keycodes on its Xlib owner, reject unavailable keys, and never rewrite the global keymap as a fallback. A new mapper can describe a changed layout, but this library does not detect changes, manage transitions or select a profile from relay data. ## Evidence and references Fixed expectations cover both transitions for modifiers, reader modifier keys, keypad/navigation, lock keys and all letter/digit/function ranges. Tests also cover generic modifiers, explicit OEM profiles, aliases, invalid/ambiguous input, independent concurrent maps, and the real JSON codec and membership direction checks. These are synthetic protocol tests; they neither consume desktop input nor exercise an X server or Windows keyboard hooks. Encoder tests additionally cover mapping changes through repeats/releases, source boundaries, modifier/lock repeat suppression, unowned ups, duplicate downs, orphan repeats, collision rejection, 32-key overflow, stale epochs, pending-write backpressure, exact acknowledgement sequences and retirement. A closed pipe verifies failed release writes retain cleanup ownership. A private TLS relay receives planned downs, repeats, original-key ups and post-collision cleanup in order. These checks exercise the production frame codec/transport without attaching the encoder to the live coordinator or to desktop services. The reader-host and both SDK process checks now cover typed incoming key IPC; see the linked reader contract for the ownership and delivery boundary. The VK values and layout dependence were checked against Microsoft's [virtual-key reference](https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes). Scan codes and extended flags are distinct Windows event metadata; see [keyboard input](https://learn.microsoft.com/en-us/windows/win32/inputdev/about-keyboard-input) and [low-level events](https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-kbdllhookstruct). The existing reader XKB bindings and system X11 keysym definitions supplied the symbol names. The local NVDA reference at `2843e703ce19757711e976ebb6e9c5b689fc2017`, [`input.sendKey`](https://github.com/nvaccess/nvda/blob/2843e703ce19757711e976ebb6e9c5b689fc2017/source/_remoteClient/input.py), was inspected for wire-field use. No mapping table or implementation was copied from NVDA or Orca. No actual NVDA frame capture, physical keyboard acceptance, desktop recovery, audible speech or Braille acceptance is claimed.