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

未關閉
#59,601 0 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 2 天內回覆

還沒有人認領這個 Issue。

評估

難度
4/5
預估耗時
3-5 天
新手友好度
54/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
android, kotlin, react-native
領域
backend, mobile

研究方向

Start with ReactModalHostView.kt, especially DialogRootViewGroup and the dialog's OnKeyListener; compare their key handling with ReactRootView.java and ReactSurfaceView.kt, where JSKeyDispatcher routes events and tracks focus. Reproduce with the linked app and enableKeyEvents enabled. Done when a focused View inside a Modal receives onKeyDown and onKeyUp, while Back or Escape still triggers onRequestClose.

由索引模型根據 Issue 內容生成。

描述

Description

With ReactNativeFeatureFlags.enableKeyEvents() on, React Native Android sends a View's onKeyDown / onKeyUp from the React root's own key dispatch: ReactRootView.dispatchKeyEvent (L327) and ReactSurfaceView.dispatchJSKeyEvent (L200), each with a JSKeyDispatcher that tracks the focused child. A <Modal> renders into a dialog window whose root is ReactModalHostView.DialogRootViewGroup (L516), which has no key dispatch and no focus tracking, so a View inside a Modal receives no key event even while it holds focus. The dialog's OnKeyListener (L313) only turns Back or Escape into onRequestClose.

Steps to reproduce

The reproducer turns enableKeyEvents on in MainApplication.kt after loadReactNative, and renders a focusable View with onKeyDown that shows the last key it got, a button that opens a <Modal>, and the same View in the Modal.

  1. Clone the reproducer; in ReproducerApp: yarn install, then cd android && ./gradlew assembleRelease and adb install -r app/build/outputs/apk/release/app-release.apk.
  2. Start the app. adb shell input keyevent KEYCODE_TAB focuses the first View, and adb shell input keyevent KEYCODE_A makes it show Outside the Modal, last key: a.
  3. Tap Open the Modal, then adb shell input keyevent KEYCODE_TAB, which focuses the View in the Modal, and adb shell input keyevent KEYCODE_A.

Expected: Inside the Modal, last key: a.
Seen: Inside the Modal, last key: none, with that View focused.

React Native Version

0.88.1

Affected Platforms

Runtime - Android

Output of npx @react-native-community/cli info
System:
  OS: Windows 11 10.0.26300
  CPU: "(32) x64 AMD Ryzen 9 9950X3D 16-Core Processor          "
  Memory: 201.56 GB / 255.67 GB
Binaries:
  Node:
    version: 26.12.1
    path: C:\Program Files\nodejs\node.EXE
  Yarn:
    version: 1.22.22
    path: C:\Users\yevhe.YEVHENII\AppData\Roaming\npm\yarn.CMD
  npm:
    version: 12.2.1
    path: C:\Program Files\nodejs\npm.CMD
  Watchman: Not Found
SDKs:
  Android SDK:
    API Levels:
      - "33"
      - "35"
      - "35"
      - "35"
      - "36"
      - "37"
    Build Tools:
      - 35.0.0
      - 36.0.0
      - 36.1.0
      - 37.0.0
    System Images:
      - android-26 | Google APIs Intel x86_64 Atom
      - android-28 | Google APIs Intel x86_64 Atom
      - android-29 | Google APIs Intel x86_64 Atom
      - android-30 | Google APIs Intel x86_64 Atom
      - android-31 | Google APIs Intel x86_64 Atom
      - android-32 | Google APIs Intel x86_64 Atom
      - android-33 | Google APIs Intel x86_64 Atom
      - android-34 | Google APIs Intel x86_64 Atom
      - android-35 | Google APIs Intel x86_64 Atom
      - android-36.1 | Google Play Intel x86_64 Atom
      - android-36.1 | Pre-Release 16 KB Page Size Google Play Intel x86_64 Atom
      - android-37.0 | Google APIs Intel x86_64 Atom
    Android NDK: Not Found
  Windows SDK: Not Found
IDEs:
  Android Studio: AI-262.9437.185.2621.16467767
  Visual Studio: Not Found
Languages:
  Java:
    version: 17.0.20.1
    path: C:\Program Files\Microsoft\jdk-17.0.20.101-hotspot\bin\javac.EXE
  Ruby: Not Found
npmPackages:
  "@react-native-community/cli":
    installed: 20.2.0
    wanted: 20.2.0
  react:
    installed: 19.3.0
    wanted: 19.3.0
  react-native:
    installed: 0.88.1
    wanted: 0.88.1
  react-native-windows: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: true
iOS:
  hermesEnabled: Not found
  newArchEnabled: Not found
Stacktrace or Logs

No error is logged. The focused node (uiautomator dump) and the text of each View after each step, on a Galaxy S23 (Android 14):

{"device":"SM-S911B, Android 14"}
{"step":"started","focused":"none","shows":["Outside the Modal, last key: none"]}
{"step":"KEYCODE_TAB","focused":"outside-target","shows":["Outside the Modal, last key: none"]}
{"step":"KEYCODE_A","focused":"outside-target","shows":["Outside the Modal, last key: a"]}
{"step":"tapped Open the Modal","focused":"none","shows":["Inside the Modal, last key: none"]}
{"step":"KEYCODE_TAB in the Modal","focused":"inside-target","shows":["Inside the Modal, last key: none"]}
{"step":"KEYCODE_A in the Modal","focused":"inside-target","shows":["Inside the Modal, last key: none"]}

On main at c12d0b3e74, ReactModalHostView.kt differs from 0.88.1 only in dismissing a dialog of a destroyed Activity and in the navigation bar contrast of a transparent one; DialogRootViewGroup is the same.

MANDATORY Reproducer

https://github.com/YevheniiKotyrlo/rn-android-modal-key-events

Screenshots and Videos

No response

主要語言
C++
星號
127k
分支
25.5k
平均合併
3 天 22 小時
30 天內合併 PR
235

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

react/react-native 的其他 Issue

查看 react/react-native 的全部 Issue

相似的 Issue

更多 C++ Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。