From ee3e71f536127295ba4ea377e618499409a2e9ba Mon Sep 17 00:00:00 2001 From: fabriziobertoglio1987 Date: Wed, 25 Aug 2021 04:55:24 -0700 Subject: [PATCH] onKeyPress event not fired with numeric keys (#29046) Summary: This issue fixes https://github.com/facebook/react-native/issues/19507 fixes https://github.com/facebook/react-native/issues/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 Pull Request resolved: https://github.com/facebook/react-native/pull/29046 Test Plan: **
CLICK TO OPEN TESTS RESULTS**

| **BEFORE** | **AFTER** | |:-------------------------:|:-------------------------:| | | |

Reviewed By: hramos, cortinico Differential Revision: D30427789 Pulled By: sshic fbshipit-source-id: b4e17ab94daa59fe28de5a5141b0fdd49bab72e3 --- .../views/textinput/ReactEditTextInputConnectionWrapper.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditTextInputConnectionWrapper.java b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditTextInputConnectionWrapper.java index 55ae040054b92e..aa2f9a6462e524 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditTextInputConnectionWrapper.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditTextInputConnectionWrapper.java @@ -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);