Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] onKeyPress event not fired with numeric keys #29046

Closed

Conversation

fabOnReact
Copy link
Contributor

@fabOnReact fabOnReact commented Jun 3, 2020

Summary

This issue fixes #19507 fixes #30475 onKeyPress event not fired for numeric keyboard
The TextInput onKeyPress event is not fired when pressing numeric keys on Android.

The method sendKeyEvent will dispatchKeyEvent only for:

  • ENTER_KEY_VALUE
  • KEYCODE_DEL (delete key)

The solution proposed is trigger dispatchKeyEvent for KeyEvents with event.getUnicodeChar() value included between 47 and 58 (numeric keys 0-9)

Changelog

[Android] [Fixed] - onKeyPress event not fired with numeric keys

Test Plan

CLICK TO OPEN TESTS RESULTS

BEFORE AFTER

@facebook-github-bot facebook-github-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 Jun 3, 2020
@react-native-bot react-native-bot added Bug Platform: Android Android applications. labels Jun 3, 2020
@fabOnReact fabOnReact changed the title log keypress events from numeric keys [Android] onKeyPress event not fired with numeric keys Jun 3, 2020
@analysis-bot
Copy link

analysis-bot commented Jun 3, 2020

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 8,891,121 +44
android hermes armeabi-v7a 8,394,854 +55
android hermes x86 9,380,376 +46
android hermes x86_64 9,323,109 +51
android jsc arm64-v8a 10,341,100 +49
android jsc armeabi-v7a 9,827,929 +53
android jsc x86 10,391,872 +59
android jsc x86_64 10,974,847 +56

Base commit: 4324ca8

@analysis-bot
Copy link

analysis-bot commented Jun 3, 2020

Platform Engine Arch Size (bytes) Diff
ios - universal n/a --

Base commit: 4324ca8

@nukemonk
Copy link

nukemonk commented Nov 21, 2020

Thank you very much for this fix! There's only one adjustment that needs to be made on line 135:

boolean isNumberKey = event.getUnicodeChar() < 56 &&

the number needs to be < 58 since event.key for the number 9 is 57. Now number 8 and 9 are excluded and don't trigger the event.

@fabOnReact
Copy link
Contributor Author

@nukemonk thanks a lot for the correction, I will make the adjustment this weekend. I wish you a good day. Fabrizio

@fabOnReact fabOnReact marked this pull request as draft December 7, 2020 08:13
@fabOnReact fabOnReact marked this pull request as ready for review December 20, 2020 15:45
Copy link

@analysis-bot analysis-bot left a comment

Choose a reason for hiding this comment

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

Code analysis results:

@@ -132,10 +132,14 @@ public boolean deleteSurroundingText(int beforeLength, int afterLength) {
@Override
public boolean sendKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
boolean isNumberKey = event.getUnicodeChar() < 58 &&

Choose a reason for hiding this comment

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

google-java-format suggested changes:

@@ -135,2 +135 @@
-      boolean isNumberKey = event.getUnicodeChar() < 58 &&
-        event.getUnicodeChar() > 47;
+      boolean isNumberKey = event.getUnicodeChar() < 58 && event.getUnicodeChar() > 47;

@mariomurrent-softwaresolutions

Any Updates on this one?

@fabOnReact
Copy link
Contributor Author

@mariomurrent-softwaresolutions I need more time and have more detailed look at the detox failures. Thanks

test-android

+ buck fetch ReactAndroid/src/test/java/com/facebook/react/modules
Not using buckd because watchman isn't installed.
Picked up _JAVA_OPTIONS: -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap
PARSING BUCK FILES: FINISHED IN 1.9s
Buck wasn't able to parse /root/react-native/ReactAndroid/src/main/java/com/facebook/BUCK:
Incorrect arguments to android_library with name yoga: Extra unknown kwargs: autoglob

This error happened while trying to get dependency '//ReactAndroid/src/main/java/com/facebook:yoga' of target '//ReactAndroid/src/test/java/com/facebook/react/modules:modules'

Exited with code exit status 1

test-docker

Not using buckd because watchman isn't installed.
Picked up JAVA_TOOL_OPTIONS: -Dfile.encoding=UTF8
PARSING BUCK FILES: FINISHED IN 3.3s
Buck wasn't able to parse /app/ReactAndroid/src/main/java/com/facebook/BUCK:
Incorrect arguments to android_library with name yoga: Extra unknown kwargs: autoglob

This error happened while trying to get dependency '//ReactAndroid/src/main/java/com/facebook:yoga' of target '//ReactAndroid/src/test/java/com/facebook/react/modules:modules'
The command '/bin/sh -c buck fetch ReactAndroid/src/test/java/com/facebook/react/modules' returned a non-zero code: 1
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Exited with code exit status 1

CircleCI received exit code 1

@mariomurrent-softwaresolutions

Okay, cool & thx

@fabOnReact fabOnReact closed this Jan 2, 2021
@fabOnReact fabOnReact deleted the numpad-keypress-not-logging branch January 2, 2021 15:52
@fabOnReact fabOnReact restored the numpad-keypress-not-logging branch January 2, 2021 15:52
@mariomurrent-softwaresolutions

@fabriziobertoglio1987 Is it done?

@fabOnReact fabOnReact reopened this Jan 3, 2021
@vadimb
Copy link

vadimb commented Jan 5, 2021

@fabriziobertoglio1987 this CI's issue you are experiencing seems to have been fixed in master. Would you mind updating your branch to master please? I'm more then happy to collaborate on this if you are busy.

@fabOnReact
Copy link
Contributor Author

fabOnReact commented Jan 5, 2021

@vadimb thanks a lot for your feedback. Feel free to do a code review on this pr and collaborate with me. I'll be happy to accept your improvements.. Currently I'm working (in my free time) on other prs and I don't know of any problems with this pr, but I'll be happy to follow your advice/improvements. Thanks a lot for finding the solution to the issue with CI Tests. Thanks a lot 🙏

@vadimb
Copy link

vadimb commented Jan 10, 2021

@fabriziobertoglio1987 I just observed that you handle ONLY digits, right? So if I were to use a phone-pad typing on non-digit key like + e.g +1 123 123 1234 wont work( I'll get only the digits).

I'm wondering what keeps us from matching the iOS implementation https://github.com/facebook/reac
t-native/blob/master/React/CoreModules/RCTEventDispatcher.mm#L85 where all keys get pass trough ?

Just tested and seems all other keys(including +) are handled properly - so your fix seems right!

Hopefully it will land ASAP 👍

@fabOnReact
Copy link
Contributor Author

thanks a lot @vadimb

yes, but still reading your review and interacting with you is very valuable for me, as from our interaction we may get ideas on how to improve or break this functionality.

I consider you comment a very valuable code review. I did not think about this scenario and use case, but luckily it was covered.
I wish you a good evening. Thanks. Fabrizio

@vadimb
Copy link

vadimb commented Jan 22, 2021

@nukemonk can you please let us know if there is any eta when we this PR will be merged?

@fabOnReact
Copy link
Contributor Author

fabOnReact commented Jan 22, 2021

@vadimb there is no eta. 🙏 😄

@nukemonk
Copy link

@nukemonk can you please let us know if there is any eta when we this PR will be merged?

@vadimb I am sorry but I am unsure about what you request of me. I am not a maintainer of this branch nor I am authorized to do anything with it.

@vadimb
Copy link

vadimb commented Jan 22, 2021

My bad - seeing that you approved thought you have super powers :) - but seems anyone can review/approve.

@mariomurrent-softwaresolutions

I already updated the code from @fabriziobertoglio1987. But no PR approve on his side

Copy link

@pepf pepf left a comment

Choose a reason for hiding this comment

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

🥇

@vastamaki
Copy link

Any estimate when this could be released? 🙂

@fabOnReact
Copy link
Contributor Author

@vastamaki you can check out https://github.com/sponsors/fabriziobertoglio1987 if you are interested, otherwise I can estimate 6-12 months.

@fabOnReact
Copy link
Contributor Author

Any estimate when this could be released?

@vastamaki To answer the question of when this fix will be released in the next version of React Native, I suggest you to follow the discussions at https://github.com/react-native-community/releases/issues by subscribing to those threads. The process of releasing a new version with a bug fix is the following:

  • an Issue is opened in facebook/react-native
  • a contributor writes a Pull Request to fix/close the react-native Issue
  • once the Pull Request is merged, the issue is closed. The review and merge is performed by the Facebook Team that works on the Facebook Ads App running React Native from source. Currently we have 351 open pull requests so the review and merge may take some time.
  • The fix is tested in Facebook Ads App running React Native from master for several months
  • For each realease (the upcoming one is 0.64.1) there will be a separate discussion on https://github.com/react-native-community/releases/issues to discuss
    In this discussion everyone can request a commit to be cherrypicked from master. The link need to be from the a commit from master (for ex. [Android] Adding Hyphenation Frequency prop for Text component #29157 (comment)) and not a Pull Request not yet merged in master ☮️ 🙏

@lfabl
Copy link

lfabl commented Apr 13, 2021

Same problem. But not only android. My problem on both. ( IOS + Android ). I don't know why pull request was technically rejected, but I have a specific need. I am faced with a case that works according to an algorithm and I have to process individual letters. Just; numbers, commas, periods and backspace. For this reason, is it possible to come up with a solution?

@facebook-github-bot facebook-github-bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Aug 19, 2021
@facebook-github-bot
Copy link
Contributor

@sshic has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

1 similar comment
@facebook-github-bot
Copy link
Contributor

@sshic has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Aug 25, 2021
@facebook-github-bot
Copy link
Contributor

@sshic merged this pull request in ee3e71f.

@Matiassimone
Copy link

Still persists,
on "react-native": "0.68.2".

Using this TextInput with the follow props and keyboardType="number-pad",
the numbers only trigger in the onKeyPress,
onChangeText or onChange callbacks are NOT called when press numbers (1, 2, 3...).

      <TextInput
        autoFocus
        caretHidden
        contextMenuHidden
        onKeyPress={onKeyPress}
        ref={phoneRef}
        maxLength={6}
        autoCorrect={false}
        onChange={onChange}
        onChangeText={onChangeText}
        keyboardType="number-pad"
        textContentType="oneTimeCode"
        autoComplete="sms-otp"
        keyboardAppearance={isDarkMode ? 'dark' : 'light'}
        style={styles.nativeKeyboardHidden}
      />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Needs: React Native Team Attention Platform: Android Android applications. 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.

Android TextInput onKeyPress not fired [Android] onKeyPress event not fired for numeric keyboard