Skip to content

Report hardware keyboard keys in Android TextInput onKeyPress - #59120

Open
YevheniiKotyrlo wants to merge 3 commits into
react:mainfrom
YevheniiKotyrlo:fix/android-hardware-key-press
Open

YevheniiKotyrlo wants to merge 3 commits into
react:mainfrom
YevheniiKotyrlo:fix/android-hardware-key-press

Conversation

@YevheniiKotyrlo

@YevheniiKotyrlo YevheniiKotyrlo commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

With a keyboard attached to an Android phone or tablet, TextInput's onKeyPress misses keys, and which ones depends on the keyboard app. With Samsung Keyboard, the default on Samsung devices, nothing typed on the keyboard is reported: not letters, not Enter, not Backspace. With Gboard, letters and Backspace are reported, but arrows, Home, End and every other key are not. A code field that steps back on Backspace, or a suggestion list moved with the arrow keys, works on the web and not on Android. The docs record the limitation ("on Android only the inputs from soft keyboard are handled"), and #19507 met the same gap on the soft numeric keypad, which #29046 closed for digits only.

onKeyPress only reported what an input method types through the InputConnection. A hardware key reaches ReactEditText as a key event instead: directly with Samsung Keyboard, and through InputConnection.sendKeyEvent with Gboard. This reports every key event from ReactEditText.onKeyDown, where both arrive, by its KeyboardEvent.key value, with altKey, ctrlKey, metaKey and shiftKey. A modifier key pressed alone (Shift, Control, Alt, Meta, Fn, Sym and the lock keys) is not reported, and neither are system keys (Back, volume, media); a character typed after a dead key is reported composed, as on the web. The input connection wrapper keeps reporting typed text, without its sendKeyEvent special cases.

The names come from the table the View onKeyDown event (#54308) already used, completed with the UI Events key values as Firefox for Android pairs them with key codes, so onKeyDown also names Home, End, the function keys and Ctrl shortcuts that it reported as Unidentified. onKeyDown alone does not cover this: it is behind enableKeyEvents, and it never sees a key that Gboard types as text.

Changelog:

[ANDROID] [ADDED] - TextInput onKeyPress reports hardware keyboard keys by their KeyboardEvent.key value, with altKey, ctrlKey, metaKey and shiftKey

Test Plan:

ReactEditTextKeyPressTest focuses the view in a window and sends keys the two ways a device does: to the window, as a hardware keyboard does, and through the view's input connection, which the framework delivers back to the view. On main 10 of its 16 cases fail; with the change all pass. KeyDownEventTest fails on main with Unidentified for Home, PageDown, Delete and F1. The ReactAndroid unit suite passes with -PenableWarningsAsErrors=true, as do Flow, both TypeScript checks, ESLint, Prettier and ktfmt, and ReactNativeApi.d.ts is regenerated.

RNTester release builds with the Playground below, typing a, Shift+B, Ctrl+C, ←, →, Home, End, F1, Alt+E, E, volume up, volume down, ←, Backspace and z on a USB keyboard (registered through uinput), report:

main this PR
Samsung SM-S911B, Android 14, Samsung Keyboard [] a, shift+B, ctrl+c, ArrowLeft, ArrowRight, Home, End, F1, alt+e, e, ArrowLeft, Backspace, z
nubia NX809J, Android 16, Gboard a, B, e, Backspace, z a, B, ctrl+c, ArrowLeft, ArrowRight, Home, End, F1, alt+e, e, ArrowLeft, Backspace, z

Gboard types letters as text and forwards the other keys, so B arrives without shiftKey. Shift, Control, Alt and the volume keys are not reported on either device. An accent key (Alt+E in Android's virtual key map, sent with adb shell input keycombination) reports alt+Dead and then é on the S23. The soft keyboard (q, Backspace, Backspace, w) and the soft numeric keypad (1, 2, Backspace, 3) report the same keys on both builds and both devices.

packages/rn-tester/js/examples/Playground/RNTesterPlayground.js
/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @flow strict-local
 * @format
 */

import type {RNTesterModuleExample} from '../../types/RNTesterTypes';
import type {TextInputKeyPressEvent} from 'react-native';

import RNTesterText from '../../components/RNTesterText';
import ExampleTextInput from '../TextInput/ExampleTextInput';
import * as React from 'react';
import {useState} from 'react';
import {StyleSheet, View} from 'react-native';

function describeKeyPress(event: TextInputKeyPressEvent): string {
  const {altKey, ctrlKey, metaKey, shiftKey, key} = event.nativeEvent;
  return [
    altKey === true ? 'alt' : null,
    ctrlKey === true ? 'ctrl' : null,
    metaKey === true ? 'meta' : null,
    shiftKey === true ? 'shift' : null,
    key,
  ]
    .filter(part => part != null)
    .join('+');
}

function KeyPressLog({
  label,
  keyboardType,
}: {
  label: string,
  keyboardType: 'default' | 'number-pad',
}): React.Node {
  const [text, setText] = useState('');
  const [keys, setKeys] = useState<ReadonlyArray<string>>([]);
  return (
    <>
      <ExampleTextInput
        testID={`${label}-input`}
        autoCapitalize="none"
        keyboardType={keyboardType}
        placeholder={label}
        onChangeText={setText}
        onKeyPress={event => {
          const description = describeKeyPress(event);
          setKeys(previousKeys => [...previousKeys, description]);
        }}
      />
      <RNTesterText>
        {label} text: {JSON.stringify(text)}
      </RNTesterText>
      <RNTesterText>
        {label} onKeyPress: {JSON.stringify(keys)}
      </RNTesterText>
    </>
  );
}

function Playground() {
  return (
    <View style={styles.container}>
      <KeyPressLog label="keys" keyboardType="default" />
      <KeyPressLog label="digits" keyboardType="number-pad" />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    padding: 10,
    rowGap: 8,
  },
});

export default {
  title: 'Playground',
  name: 'playground',
  description: 'Test out new features and ideas.',
  render: (): React.Node => <Playground />,
} as RNTesterModuleExample;

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 15, 2026
@YevheniiKotyrlo
YevheniiKotyrlo marked this pull request as ready for review October 15, 2026 18:51
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Oct 15, 2026
@YevheniiKotyrlo
YevheniiKotyrlo marked this pull request as draft October 15, 2026 20:09
@github-actions

Copy link
Copy Markdown

Warning

JavaScript API change detected

This PR commits an update to ReactNativeApi.d.ts, indicating a change to React Native's public JavaScript API.

  • Please include a clear changelog message.
  • This change will be subject to additional review.

This change was flagged as: POTENTIALLY_BREAKING

@YevheniiKotyrlo

Copy link
Copy Markdown
Contributor Author

@Abbondanzo with a hardware keyboard on Android, TextInput's onKeyPress reports nothing under Samsung Keyboard and only letters and Backspace under Gboard; this reports the keys by their KeyboardEvent.key value with the modifiers held, as on the web.

@YevheniiKotyrlo
YevheniiKotyrlo force-pushed the fix/android-hardware-key-press branch from 0725a33 to 4380cb6 Compare November 26, 2026 13:33
@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 Nov 26, 2026
@Abbondanzo

Copy link
Copy Markdown
Contributor

shift+Shift

We shouldn't be reporting modifier-only key presses, that's not something that web does

@YevheniiKotyrlo
YevheniiKotyrlo marked this pull request as draft November 30, 2026 21:06
@YevheniiKotyrlo
YevheniiKotyrlo force-pushed the fix/android-hardware-key-press branch from 4380cb6 to dfa20e1 Compare November 30, 2026 23:23
@YevheniiKotyrlo

Copy link
Copy Markdown
Contributor Author

Modifier keys pressed alone are no longer reported: Shift, Control, Alt, Meta, Fn, Sym and the lock keys reach onKeyPress only as the shiftKey, ctrlKey, altKey and metaKey of the key they are held with, whether the keyboard sends them to the view or the input method forwards them. modifierKeysPressedAloneAreNotReported covers both paths, and the table in the description is from RNTester builds of the new head.

@YevheniiKotyrlo
YevheniiKotyrlo marked this pull request as ready for review November 30, 2026 23:24
@meta-codesync

meta-codesync Bot commented Nov 30, 2026

Copy link
Copy Markdown

@Abbondanzo has imported this pull request. If you are a Meta employee, you can view this in D135204282.

@javache javache left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review automatically exported from Phabricator review in Meta.

@Abbondanzo

Copy link
Copy Markdown
Contributor

@YevheniiKotyrlo could you regenerate ReactAndroid.api? The script is available in via Gradle's :packages:react-native:ReactAndroid:apiDump

@YevheniiKotyrlo

Copy link
Copy Markdown
Contributor Author

Added in ed74448: ReactEditText gains public fun onKeyDown (ILandroid/view/KeyEvent;)Z, the only line this change adds to ReactAndroid.api.

apiDump is not registered in the OSS build, so the open-source path of yarn android-api-build in #60444 hits the same wall: since #57038 ReactAndroid applies no kotlin-android plugin for binary-compatibility-validator 0.13.2 to hook, and 0.13.2 cannot read Kotlin 2.4 metadata either. I dumped with org.jetbrains.kotlin:abi-tools:2.4.21 under the repository's apiValidation filters; its other differences from the committed file are all in classes this PR does not touch, so they are left out.

The missing task is #60446.

@Abbondanzo

Copy link
Copy Markdown
Contributor

@YevheniiKotyrlo can you rebase to fix merge conflicts?

@YevheniiKotyrlo
YevheniiKotyrlo force-pushed the fix/android-hardware-key-press branch from ed74448 to 369364f Compare December 3, 2026 20:27
ReactEditText.onKeyDown reports every key event the view receives, from
a hardware keyboard or forwarded by an input method through
InputConnection.sendKeyEvent, by its KeyboardEvent.key value, with
altKey, ctrlKey, metaKey and shiftKey. A Ctrl or Meta shortcut is named
by its key, and so is an Alt shortcut where Alt types nothing on the
layout. A key that types a character after a dead key is reported as
the composed character, and system keys such as Back and volume are not
reported. ReactEditTextInputConnectionWrapper reports only the text an
input method types, so no key is reported twice.

The key table follows the UI Events key values as Firefox for Android
pairs them with key codes, so View onKeyDown also names Home, End, the
function keys and Ctrl shortcuts instead of reporting them as
Unidentified.

Signed-off-by: YevheniiKotyrlo <44449990+YevheniiKotyrlo@users.noreply.github.com>
…ress

A modifier key pressed alone - Shift, Control, Alt, Meta, Fn, Sym, Num
and the lock keys - types nothing, so onKeyPress does not report it,
whether the keyboard sends it to the view or the input method forwards
it. It reaches onKeyPress as the altKey, ctrlKey, metaKey and shiftKey
of the key it is held with. View onKeyDown keeps reporting it.

Signed-off-by: YevheniiKotyrlo <44449990+YevheniiKotyrlo@users.noreply.github.com>
Signed-off-by: YevheniiKotyrlo <44449990+YevheniiKotyrlo@users.noreply.github.com>
@YevheniiKotyrlo
YevheniiKotyrlo force-pushed the fix/android-hardware-key-press branch from 369364f to 384dfaf Compare December 4, 2026 18:13
@YevheniiKotyrlo

YevheniiKotyrlo commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor Author

Rebased onto main (e812580); the generated ReactNativeApi.d.ts was the one conflict, regenerated with yarn build-types. The commits now carry Signed-off-by for the DCO check.

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

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants