facebook/react-native

Android onKeyPress not triggered

Closed this issue Β· 22 comments

Hello,

on a TextInput, onKeyPress event isn't being triggered when some keys are pressed.
This behavior happened in this test when I set a keyBoardType={"phone-pad"}, each number key on the pad, when it is pressed, not trigger the onKeyPress event, the other keys such backspace, etc, trigger the event.

Environment

React Native 0.54

Expected Behavior

Triggering the event onKeyPress when a key is pressed.

Actual Behavior

Triggering only when some keys are pressed

Steps to Reproduce

<TextInput
blurOnSubmit={true}
returnKeyType={"done"}
onKeyPress={(e)=> { console.log("onKeyPress")} } />

Regards.

Thanks for posting this! It looks like your issue may be incomplete. Are all the fields required by the Issue Template filled out?

You may safely ignore this if you believe your issue contains all the relevant information. Thank you for your contributions.

How to Contribute β€’ What to Expect from Maintainers

Thanks for posting this! It looks like your issue may be missing some necessary information. Can you run react-native info and edit your issue to include these results under the Environment section?

Thank you for your contributions.

Closing after seven days with no follow up from the author.

Please open this issue.
I face the same problem as the author. (android problem)
onKeyPress only works well with keyboardType="default", otherwise onKeyPress cannot trigger the key event (only backspace and some keys can be triggered).
Could you please handle onKeyPress working properly on any keyboardType as iOS?

Here is my react-native info:

Environment:
OS: macOS High Sierra 10.13.4
Node: 8.11.1
Yarn: 1.5.1
npm: 5.6.0
Watchman: 4.9.0
Xcode: Xcode 9.3 Build version 9E145
Android Studio: 3.1 AI-173.4697961

Packages: (wanted => installed)
react: ^16.3.0-alpha.1 => 16.3.0-alpha.3
react-native: 0.54.2 => 0.54.2

I'm encountering the issue as @lamnguyen2307 and @araujobarret. I cannot get onKeyPress={} to fire from a <TextInput /> except when the keyboardType is default.

+1 Doesn't matter what keyboardtype it just doesn't fire.

πŸ‘ Not working here either, I'm on latest 0.56 rc3

I too am experiencing it, keyboardType has no influence. Doesn't fire at all. On Android, RN 0.55.4. Hopefully we can get this issue opened back up.

Same issue here, but on iOS. onKeyPress is just not firing.

same issue here, works on IOS but no on android

I just came across with the same problem! My problem was me trying to fire onKeyPress from the PC keyboard and not from the emulator/device πŸ˜’

have the same issue, any ideas?

Same issue…

make sure ya'll aren't using your hardware keyboard but rather use the emulators keyboard. this worked for me

Same issue

Same for me ! onKeyPress work on iOS but not on my Android 7.0 device except when the keyboardType="default"...

Since upgrading to React Native 0.56:

Android 7.0 and up onKeyPress works
Android 6.0 and below onKeyPress ONLY letters, not numbers.

Same issue, onKeyPress not work on android, doesn't matter what keyboardtype, all not fire

Quick fix :

In MainActivity.java

    public boolean onKeyUp(int keyCode, KeyEvent event) {
        super.onKeyUp(keyCode, event);
        WritableMap payload = Arguments.createMap();
        payload.putString("keyCode", KeyEvent.keyCodeToString(event.getKeyCode()));
        payload.putString("unicodeChar", String.valueOf(event.getUnicodeChar()));
        payload.putString("displayLabel", String.valueOf(event.getDisplayLabel()));
        ReactContext reactContext = getReactNativeHost().getReactInstanceManager().getCurrentReactContext();
        if (reactContext != null){
            reactContext
                    .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                    .emit("onKeyPressed", payload);
        }
        return true;
    }
  onKeyPressed = evt => {
    if (!this.textInputRef.current.isFocused()) {
      return;
    }
    const { displayLabel: key, keyCode } = evt;
    if (keyCode === 'KEYCODE_DEL') {
      return this.props.onKeyPress({ nativeEvent: { key: 'Backspace' } });
    }
    this.props.onKeyPress({ nativeEvent: { key } });
  }
   componentDidMount = () => this.$onKeyPressedSubscription = DeviceEventEmitter.addListener('onKeyPressed', this.onKeyPressed);

   componentWillUnmount = () => this.$onKeyPressedSubscription.remove();

PS: This workaround works with Goolge Keyboard but not with Samsung one

Hello,

Do you have found any solution for this. As I am facing this issue on Android 6.0.

The issue is happening for me too. Please re open this issue.

Same here. Whatever be the keyboard type, onKeyPress doesn't trigger on android.