Skip to content

Fix key events not reaching views inside Modal on Android - #60505

Open
ykdhan wants to merge 1 commit into
react:mainfrom
ykdhan:fix/modal-key-events
Open

ykdhan wants to merge 1 commit into
react:mainfrom
ykdhan:fix/modal-key-events

Conversation

@ykdhan

@ykdhan ykdhan commented Dec 3, 2026 •

Copy link
Copy Markdown

Summary:

Fixes #59601.

With ReactNativeFeatureFlags.enableKeyEvents() on, React Native Android sends onKeyDown / onKeyUp from the React root's own key dispatch: ReactRootView.dispatchKeyEvent and ReactSurfaceView, each with a JSKeyDispatcher that tracks the focused child. A <Modal> is shown in its own dialog window, and that window's root, ReactModalHostView.DialogRootViewGroup, had no key dispatch and no focus tracking. As a result, a View inside a Modal never received key events, even while it held focus.

This change gives DialogRootViewGroup the same handling ReactSurfaceView has:

  • It creates a JSKeyDispatcher when enableKeyEvents is on.
  • It tracks the focused child in requestChildFocus / onFocusChanged.
  • It sends keydown/keyup to JS from dispatchKeyEvent before children handle the event, the same capture-phase order ReactRootView uses.

The dialog's OnKeyListener runs before the window's views see an event, and it consumes some key ups:

  • BACK and ESCAPE, which it turns into onRequestClose.
  • Keys the activity's onKeyUp handles: the dev menu key, and double-tap R in debug builds.

Those key ups never reach DialogRootViewGroup, so the listener now forwards them to JS itself. This way JS gets a keyup for each keydown it received. One exception predates this change: on API 36, Dialog.onKeyDown already dismisses a cancelable dialog on ESCAPE keydown, before the listener runs. In that case the ESCAPE keyup never arrives (see Test Plan).

When enableKeyEvents is off, jSKeyDispatcher stays null and behavior is unchanged.

Changelog:

[ANDROID] [FIXED] - Fix views inside Modal not receiving onKeyDown / onKeyUp when key events are enabled

Test Plan:

Unit tests. A new Robolectric test, DialogRootViewGroupKeyEventsTest, covers these cases:

  • keydown and keyup reach the focused view, with the right view tag and surface id;
  • moving focus retargets the events;
  • dispatchJSKeyEvent (the path the dialog's key listener uses) reaches the focused view;
  • nothing is dispatched when no view is focused, when enableKeyEvents is off, or when there is no event dispatcher.
./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest --tests "com.facebook.react.views.modal.DialogRootViewGroupKeyEventsTest"

BUILD SUCCESSFUL; the JUnit report shows tests="6" skipped="0" failures="0" errors="0".

Neighboring tests still pass:

./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest --tests "com.facebook.react.views.modal.*" --tests "com.facebook.react.uimanager.JSPointerDispatcherTest"

BUILD SUCCESSFUL: DialogRootViewGroupKeyEventsTest 6/6, JSPointerDispatcherTest 1/1. I did not run the full testDebugUnitTest suite.

Public API snapshot. DialogRootViewGroup gains two public overrides, dispatchKeyEvent and requestChildFocus. yarn android-api-build regenerated ReactAndroid.api with no diff against the committed file.

Formatting. yarn format-check-kotlin passed.

Lint. yarn lint passed.

JS API snapshot. yarn build-types --validate passed: API snapshot is up to date.

Manual check. I used RNTester built from source on an Android emulator (API 36, arm64-v8a), not the reproducer from #59601, because it pins the published 0.88.1. In a local-only change that is not part of this PR, I turned enableKeyEvents on in RNTesterApplication and replaced the Playground with the reproducer's layout: a focusable View with onKeyDown outside a <Modal>, the same View inside it, and an on-screen log of keydown, keyup and onRequestClose. I sent keys with adb shell input keyevent.

  1. Outside the Modal, KEYCODE_TAB then KEYCODE_A: the View shows last key: a and logs keydown and keyup for a, before and after this change.
  2. Open the Modal, then KEYCODE_TAB then KEYCODE_A: with this change, the View inside the Modal shows last key: a and logs keydown and keyup. Before this change it received nothing.
  3. Inside the Modal, KEYCODE_BACK: with this change, the focused View gets keydown, then onRequestClose is called, then the View gets keyup. Before this change onRequestClose was called and the View got no key events.

KEYCODE_ESCAPE could not be verified on this emulator. On API 36, Dialog.onKeyDown cancels a cancelable dialog on ESCAPE keydown, before this listener (which only handles key ups) sees the event. The dialog is dismissed natively: onRequestClose is not called and the ESCAPE keyup never reaches the dialog. That happens the same way before and after this change. With this change, JS additionally receives the ESCAPE keydown. That is out of scope here, and I can file it separately.

Before (no key events inside the Modal)
https://github.com/user-attachments/assets/21c2e96c-b34f-4dcc-a46e-a32d36b4d7e2

After
https://github.com/user-attachments/assets/a965a8c6-e2ac-4057-ab69-c63fd86cf857

@github-actions github-actions Bot added the Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. label Dec 3, 2026
@ykdhan
ykdhan force-pushed the fix/modal-key-events branch from 31384e3 to bc95fca Compare December 3, 2026 12:39
With enableKeyEvents on, React Native Android sends onKeyDown/onKeyUp
from the React root's own key dispatch (ReactRootView, ReactSurfaceView).
A Modal is shown in its own dialog window whose root,
ReactModalHostView.DialogRootViewGroup, had no key dispatch and no focus
tracking, so a focused View inside a Modal never received key events.

Give DialogRootViewGroup the same JSKeyDispatcher handling that
ReactSurfaceView has: track the focused child, and send keydown/keyup to
JS from dispatchKeyEvent before children handle the event.

The dialog's OnKeyListener consumes some key ups before they reach the
dialog's views: BACK/ESCAPE (which close the Modal) and keys the activity
handles (dev menu, double-tap R). Forward those key ups to JS too, so JS
gets a keyup for every keydown it received.

Fixes react#59601

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: YK <21373658+ykdhan@users.noreply.github.com>

This branch has not been deployed

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

Labels

Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Android: with enableKeyEvents on, a focused View inside a <Modal> never receives onKeyDown

1 participant