Skip to content

Commit

Permalink
onKeyPress event not fired with numeric keys (#29046)
Browse files Browse the repository at this point in the history
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

<!-- Help reviewers and the release process by writing your own changelog entry. For an example, see:
https://github.com/facebook/react-native/wiki/Changelog
-->

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

Pull Request resolved: #29046

Test Plan:
**<details><summary>CLICK TO OPEN TESTS RESULTS</summary>**
<p>

| **BEFORE** | **AFTER** |
|:-------------------------:|:-------------------------:|
| <img src="https://user-images.githubusercontent.com/24992535/83673015-7ce2a000-a5d7-11ea-9c1d-32a5f5605687.gif" width="300" height="" /> | <img src="https://user-images.githubusercontent.com/24992535/83673017-7f44fa00-a5d7-11ea-8d93-edf1f61f7023.gif"  width="300" height="" /> |

</p>
</details>

Reviewed By: hramos, cortinico

Differential Revision: D30427789

Pulled By: sshic

fbshipit-source-id: b4e17ab94daa59fe28de5a5141b0fdd49bab72e3
  • Loading branch information
fabOnReact authored and facebook-github-bot committed Aug 25, 2021
1 parent 4841e1b commit ee3e71f
Showing 1 changed file with 3 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,13 @@ public boolean deleteSurroundingText(int beforeLength, int afterLength) {
@Override
public boolean sendKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
boolean isNumberKey = event.getUnicodeChar() < 58 && event.getUnicodeChar() > 47;
if (event.getKeyCode() == KeyEvent.KEYCODE_DEL) {
dispatchKeyEvent(BACKSPACE_KEY_VALUE);
} else if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
dispatchKeyEvent(ENTER_KEY_VALUE);
} else if (isNumberKey) {
dispatchKeyEvent(String.valueOf(event.getNumber()));
}
}
return super.sendKeyEvent(event);
Expand Down

0 comments on commit ee3e71f

Please sign in to comment.