Skip to content

feat(platform): deliver mobile hardware key callbacks - #1974

Merged
houko merged 1 commit into
nsgo-dev:mainfrom
houko:feat/native-keys-1352
Nov 23, 2026
Merged

houko merged 1 commit into
nsgo-dev:mainfrom
houko:feat/native-keys-1352

Conversation

@houko

@houko houko commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

Summary

Android now delivers View.OnKeyDown before bindings and native control actions, waits asynchronously for its consume answer, and reports OnKeyUp to the view that received the down. core.KeysDown follows hardware keys outside native editors. Previously Android reported only shortcut bindings, so focused controls could not consume a physical key or observe its release.

Depends on #1966. This branch carries that focus/activation commit rebased onto main (cb5dc33d4); this PR's own change is 810c9a1f0. The existing UIKit key dispatcher works for explicitly focused iPhone/iPad views once that dependency supplies native focus. New native regressions verify it on both idioms. Merge/rebase the dependency first; this PR does not replace its activation implementation. The software-Return and editing-key changes in #1967 and #1354 remain separate, as does native Select in #1968.

Java captures each key's original focus and queues a correlated continuation. Go enqueues DispatchKeyDownAsync before reading subsequent releases, while leaving its reader available for text replies. Unconsumed events run the existing keymap/activation/native dispatch, with native Tab/arrow focus search preserved. Consumed downs still get their releases. Modifiers, repeats, focus loss, teardown and stale/dropped replies are covered; a two-second watchdog abandons pending native actions and releases observed keys rather than leaving them held. ItemKeymap carries the held-key subscription, and EventKeyUp remains outside the event mask.

HarmonyOS's Go bridge now orders callbacks before Go bindings, tracks held keys/repeats and original release targets, preserves native editor focus across late blur or unsupported plain-view focus requests, and clears held keys on lifecycle loss. The issue remains open: its committed ArkTS host still lacks native plain-view focus and returns before Go can consume an ArkUI action. The PR neither claims native consumption there nor modifies/rebuilds that host without an available SDK. These gaps remain in the roadmap.

Refs #1352.

Targets

Screenshots

None. These are hardware-key delivery and test-driver changes, verified through native callbacks, actions, focus and held-key assertions.

Verification

  • go tool nsgo check (host and dev client).
  • go tool nsgo check -target ios_simulator,android,harmonyos,web -jobs 2; repeated after rebase.
  • Native Android API 35 arm64 emulator: four cases cover consumed/unconsumed actions, consumed ups, held modifiers and platform aliases, repeats, releases after focus moves, native editor exclusion, real window-focus loss, and consumed/unconsumed native Tab traversal.
  • iOS 18.4 simulators: two native cases passed on both iPhone 16 and iPad Air 11-inch (M3), covering callbacks, consume-before-actions, original-target releases, held modifiers and editor exclusion. UIKit's no-repeat behavior is asserted.
  • All engine tests: 51 passed. Async ordering plus existing sync/closed-engine/release cases were repeated after rebase (3 passed).
  • nsgotest tests: 168 passed, 1 existing skip.
  • JVM continuation fixture plus wire/reader contract checks: 4 passed, repeated after resolving the additive shader-release protocol changes from main. Full Java runtime rebuild passed with both message handlers present; message IDs remain distinct (shader release 25, key reply 28, hardware key 29).
  • HarmonyOS JS/Wasm suite with nsgotest: 6 passed, repeated after rebase. The actual engine/bridge regression covers delayed queue processing, captured focus, Go binding consumption, modifiers/repeats, original releases, editor exclusion and lifecycle loss.
  • Layering, app-author skill and changelog-fragment checks: 3 passed.

No physical device or native HarmonyOS acceptance was performed. The Android runtime was rebuilt locally for native tests; DEX/JAR changes are restored and left to the repository's post-merge runtime workflow. This task's simulators/emulator and AVD are removed after verification; shared devices and caches are untouched.

Checklist

  • Conventional Commit title.
  • Architecture/public English and Chinese docs, app-author skill, roadmap and changelog updated.
  • No new third-party dependencies or assets.

@github-actions github-actions Bot added size/XL enhancement New feature or request documentation Improvements or additions to documentation iOS Android core base labels Nov 22, 2026
@github-actions github-actions Bot added the conflict Conflicts with the base branch; resolve it first label Nov 22, 2026
@houko

houko commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator Author

Not merged: this needs a design pass against main before it can be rebased.

  • The dependency fix(mobile): activate focused pressables from hardware keys #1966 is merged now, so cb5dc33d4 drops out on a rebase.
  • Main has since gained its own Android hardware-key path. 3f5193799 ("feat(core): expose physical keyboard positions") added NsgoView.physicalKey, which runs from dispatchKeyEvent and sends PHYSICAL_KEY (Java-to-Go 30). Go's surface.physicalKey (internal/platform/android/keycodes.go/android.go) then dispatches EventKeyDown/EventKeyUp to the focused node with EVENT_KEY, plus held-key reports for core.KeysDown, with scan-code KeyCodes. It is fire-and-forget, so nothing can consume the key.
  • This PR's HardwareKeys path delivers the same OnKeyDown/OnKeyUp with an async consume answer. Applied as-is on top of main, each key would reach the app twice and the two held-key sets would diverge. The two need to become one path, most likely HardwareKeys carrying the scan code and KeyCode that physicalKey reports now, with physicalKey removed or reduced to the held-key report.
  • When it is rebased, the protocol numbers change: msgKeyReply = 28 becomes extended kind extKeyReply = 12 / EXT_KEY_REPLY = 12, because Go-to-Java kinds now go through msgExtended, and errNoExtendedMessages means hardware-key consume is unsupported. msgHardwareKey / HARDWARE_KEY becomes Java-to-Go 41, because 29 is now ASSET_REPLY.

Android: HardwareKeys.java now carries main's physical-key path, so each key reaches the app once. A down that a focused view hears is held while Go answers: PHYSICAL_KEY appends a request id that DispatchKeyDownAsync answers with EXT_KEY_REPLY (12), and only an unconsumed down runs the keymap, activation and native dispatch. Releases go to the view that heard the down, held keys follow core.KeysDown, and focus loss, teardown or a two-second watchdog release them. A runtime that predates the request reports keys as before.

HarmonyOS: the Go key bridge runs view callbacks before its Go keymap, tracks repeats and original release targets, and releases keys as the lifecycle leaves active.

Refs nsgo-dev#1352
@houko
houko force-pushed the feat/native-keys-1352 branch from 7a9b07a to 911a11c Compare November 23, 2026 02:01
@github-actions github-actions Bot added the cli label Nov 23, 2026
@houko

houko commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator Author

Reworked onto main's hardware-key path, so each key now reaches the app once:

  • There is now one Android path. Main's NsgoView.physicalKey machinery (held map, PHYSICAL_KEY reports, scan-code KeyCodes) has moved into HardwareKeys.java, keyed by main's device+scan identity, and it keeps this PR's queueing, consume and continuation logic. A down that a focused key view hears is held. PHYSICAL_KEY (Java-to-Go 30) appends the request id, and Go's physicalKey reads it only if bytes remain, then answers through DispatchKeyDownAsync with EXT_KEY_REPLY = 12. Only an unconsumed down then runs the keymap, activation and native dispatch, and the keys after it wait behind it. Java-to-Go 41 was not needed. A runtime that predates the request keeps reporting keys as before.
  • Main's held-keys flag in ItemKeymap is kept as is; this PR's duplicate flag at the head of the item, which would have broken the wire, is dropped. Main's text-input exclusion (KEY_USE_TEXT) and keyboard-source check stay. Repeats are reported to held keys only when a focused view hears them.
  • The native continuation runs inside the test input gate's scripted window, and the test keyboard's events carry SOURCE_KEYBOARD. KeyDown/KeyUp/BlurWindow now live in testsurface.go, and the changelog says what this adds over main.
  • HarmonyOS's bridge sits on main's reportFocus/reportBlur, with nativeFocused tracked in both.
  • CI: the android job gains -run '^Test(Mobile|Android)HardwareKey' ./internal/testhost/testdata/keys. mobilekeys_test compares keys without the physical Code. EventKeyUp is dropped from HarmonyOS's unsupported-event group now that the bridge dispatches it.

Verified: the android CI job is green on 7a9b07a (this head only adds the HarmonyOS coverage-list fix and a rebase), including the new mobile key tests and main's TestHardwareKeys*. On Linux, go vet ./... and NSGO_REQUIRE_JDK=1 go test ./internal/platform/android/ ./internal/engine/ ./cmd/nsgo/harmony/ pass. HarmonyOS's wasm tests pass on macOS, nsgo check -target android|harmonyos|ios passes, and a full javac of the runtime succeeds.

@houko
houko merged commit d9187a1 into nsgo-dev:main Nov 23, 2026
17 of 19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Android base ci cli conflict Conflicts with the base branch; resolve it first core documentation Improvements or additions to documentation enhancement New feature or request iOS size/L

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant