Skip to content

Commit dfa20e1

Browse files
Do not report modifier keys pressed alone in Android TextInput onKeyPress
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.
1 parent e08f6e4 commit dfa20e1

6 files changed

Lines changed: 56 additions & 13 deletions

File tree

‎packages/react-native/Libraries/Components/TextInput/AndroidTextInputNativeComponent.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,9 +438,9 @@ export type AndroidTextInputNativeProps = Readonly<{
438438
* where `keyValue` is `'Enter'` or `'Backspace'` for respective keys and
439439
* the typed-in character otherwise including `' '` for space.
440440
* On Android, keys pressed on a hardware keyboard are reported as well, each
441-
* by its `KeyboardEvent.key` value (such as `'ArrowLeft'`, `'Shift'` or
441+
* by its `KeyboardEvent.key` value (such as `'ArrowLeft'`, `'Home'` or
442442
* `'F1'`), with `altKey`, `ctrlKey`, `metaKey` and `shiftKey` set for the
443-
* modifier keys held.
443+
* modifier keys held. A modifier key pressed alone is not reported.
444444
* Fires before `onChange` callbacks.
445445
*/
446446
onKeyPress?: ?BubblingEventHandler<

‎packages/react-native/Libraries/Components/TextInput/TextInput.flow.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -855,9 +855,9 @@ type TextInputBaseProps = Readonly<{
855855
* where `keyValue` is `'Enter'` or `'Backspace'` for respective keys and
856856
* the typed-in character otherwise including `' '` for space.
857857
* On Android, keys pressed on a hardware keyboard are reported as well, each
858-
* by its `KeyboardEvent.key` value (such as `'ArrowLeft'`, `'Shift'` or
858+
* by its `KeyboardEvent.key` value (such as `'ArrowLeft'`, `'Home'` or
859859
* `'F1'`), with `altKey`, `ctrlKey`, `metaKey` and `shiftKey` set for the
860-
* modifier keys held.
860+
* modifier keys held. A modifier key pressed alone is not reported.
861861
* Fires before `onChange` callbacks.
862862
*/
863863
onKeyPress?: ?(e: TextInputKeyPressEvent) => unknown,

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/KeyValue.kt‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,23 @@ internal object KeyValue {
4343

4444
fun isDeadKey(character: Int): Boolean = (character and KeyCharacterMap.COMBINING_ACCENT) != 0
4545

46+
fun isModifierKey(keyCode: Int): Boolean =
47+
AndroidKeyEvent.isModifierKey(keyCode) || keyCode in LOCK_KEYS
48+
4649
private const val SHORTCUT_MODIFIERS: Int =
4750
AndroidKeyEvent.META_CTRL_MASK or AndroidKeyEvent.META_META_MASK
4851
private const val ALT_MODIFIERS: Int = AndroidKeyEvent.META_ALT_MASK
4952
private const val DEAD: String = "Dead"
5053
private const val UNIDENTIFIED: String = "Unidentified"
5154

55+
// UI Events lists lock keys as modifiers: https://www.w3.org/TR/uievents-key/#keys-modifier
56+
private val LOCK_KEYS: Set<Int> =
57+
setOf(
58+
AndroidKeyEvent.KEYCODE_CAPS_LOCK,
59+
AndroidKeyEvent.KEYCODE_NUM_LOCK,
60+
AndroidKeyEvent.KEYCODE_SCROLL_LOCK,
61+
)
62+
5263
private val NAMED_KEYS: Map<Int, String> by
5364
lazy(LazyThreadSafetyMode.PUBLICATION) {
5465
mapOf(

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.kt‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,8 @@ public open class ReactEditText public constructor(context: Context) : AppCompat
387387

388388
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
389389
val character = KeyValue.typedCharacter(event)
390-
if (onKeyPress && !event.isSystem) {
390+
val isModifierKey = KeyValue.isModifierKey(keyCode)
391+
if (onKeyPress && !event.isSystem && !isModifierKey) {
391392
eventDispatcher?.dispatchEvent(
392393
ReactTextInputKeyPressEvent(
393394
UIManagerHelper.getSurfaceId(this),
@@ -398,8 +399,7 @@ public open class ReactEditText public constructor(context: Context) : AppCompat
398399
)
399400
}
400401
val handled = super.onKeyDown(keyCode, event)
401-
isDeadKeyPending =
402-
KeyValue.isDeadKey(character) || (isDeadKeyPending && KeyEvent.isModifierKey(keyCode))
402+
isDeadKeyPending = KeyValue.isDeadKey(character) || (isDeadKeyPending && isModifierKey)
403403
return handled
404404
}
405405

‎packages/react-native/ReactAndroid/src/test/java/com/facebook/react/views/textinput/ReactEditTextKeyPressTest.kt‎

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,22 +189,35 @@ class ReactEditTextKeyPressTest {
189189
pressHardwareKey(KeyEvent.KEYCODE_DPAD_LEFT)
190190
pressHardwareKey(KeyEvent.KEYCODE_MOVE_HOME)
191191
pressHardwareKey(KeyEvent.KEYCODE_F1)
192-
pressHardwareKey(KeyEvent.KEYCODE_SHIFT_LEFT)
193192
pressHardwareKey(KeyEvent.KEYCODE_BUTTON_A)
194193

195-
assertThat(keys())
196-
.containsExactly("a", "7", " ", "ArrowLeft", "Home", "F1", "Shift", "Unidentified")
194+
assertThat(keys()).containsExactly("a", "7", " ", "ArrowLeft", "Home", "F1", "Unidentified")
197195
}
198196

199197
@Test
200198
fun hardwareKeyIsReportedWithTheModifiersHeldWithIt() {
201-
pressHardwareKey(KeyEvent.KEYCODE_A, KeyEvent.META_SHIFT_ON or KeyEvent.META_SHIFT_LEFT_ON)
202-
pressHardwareKey(KeyEvent.KEYCODE_Z, KeyEvent.META_CTRL_ON or KeyEvent.META_CTRL_LEFT_ON)
199+
pressHardwareKey(KeyEvent.KEYCODE_SHIFT_LEFT, SHIFT_HELD)
200+
pressHardwareKey(KeyEvent.KEYCODE_A, SHIFT_HELD)
201+
pressHardwareKey(KeyEvent.KEYCODE_CTRL_LEFT, CTRL_HELD)
202+
pressHardwareKey(KeyEvent.KEYCODE_Z, CTRL_HELD)
203203

204204
assertThat(keyPresses.map { it.key to it.heldModifiers })
205205
.containsExactly("A" to setOf("shiftKey"), "z" to setOf("ctrlKey"))
206206
}
207207

208+
@Test
209+
fun modifierKeysPressedAloneAreNotReported() {
210+
MODIFIER_KEY_CODES.forEach { pressHardwareKey(it) }
211+
sendFromInputMethod(
212+
openInputConnection(),
213+
KeyEvent.KEYCODE_CTRL_LEFT,
214+
KeyEvent.FLAG_FROM_SYSTEM,
215+
)
216+
deliverPendingInput()
217+
218+
assertThat(keyPresses).isEmpty()
219+
}
220+
208221
@Test
209222
fun characterTypedAfterADeadKeyIsReportedComposed() {
210223
val acuteOnAltE = { metaState: Int ->
@@ -342,5 +355,24 @@ class ReactEditTextKeyPressTest {
342355
val MODIFIER_FIELDS = listOf("altKey", "ctrlKey", "metaKey", "shiftKey")
343356
const val SOFT_KEYBOARD_FLAGS = KeyEvent.FLAG_SOFT_KEYBOARD or KeyEvent.FLAG_KEEP_TOUCH_MODE
344357
const val ALT_HELD = KeyEvent.META_ALT_ON or KeyEvent.META_ALT_LEFT_ON
358+
const val CTRL_HELD = KeyEvent.META_CTRL_ON or KeyEvent.META_CTRL_LEFT_ON
359+
const val SHIFT_HELD = KeyEvent.META_SHIFT_ON or KeyEvent.META_SHIFT_LEFT_ON
360+
val MODIFIER_KEY_CODES =
361+
listOf(
362+
KeyEvent.KEYCODE_SHIFT_LEFT,
363+
KeyEvent.KEYCODE_SHIFT_RIGHT,
364+
KeyEvent.KEYCODE_CTRL_LEFT,
365+
KeyEvent.KEYCODE_CTRL_RIGHT,
366+
KeyEvent.KEYCODE_ALT_LEFT,
367+
KeyEvent.KEYCODE_ALT_RIGHT,
368+
KeyEvent.KEYCODE_META_LEFT,
369+
KeyEvent.KEYCODE_META_RIGHT,
370+
KeyEvent.KEYCODE_FUNCTION,
371+
KeyEvent.KEYCODE_SYM,
372+
KeyEvent.KEYCODE_NUM,
373+
KeyEvent.KEYCODE_CAPS_LOCK,
374+
KeyEvent.KEYCODE_NUM_LOCK,
375+
KeyEvent.KEYCODE_SCROLL_LOCK,
376+
)
345377
}
346378
}

‎packages/react-native/types_DEPRECATED/Libraries/Components/TextInput/TextInput.d.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ export interface TextInputProps
878878
* where keyValue is 'Enter' or 'Backspace' for respective keys and the typed-in character otherwise including ' ' for space.
879879
*
880880
* Fires before onChange callbacks.
881-
* On Android, keys pressed on a hardware keyboard are reported as well, each by its KeyboardEvent.key value (such as 'ArrowLeft', 'Shift' or 'F1'), with altKey, ctrlKey, metaKey and shiftKey set for the modifier keys held.
881+
* On Android, keys pressed on a hardware keyboard are reported as well, each by its KeyboardEvent.key value (such as 'ArrowLeft', 'Home' or 'F1'), with altKey, ctrlKey, metaKey and shiftKey set for the modifier keys held. A modifier key pressed alone is not reported.
882882
*/
883883
onKeyPress?: ((e: TextInputKeyPressEvent) => void) | undefined;
884884

0 commit comments

Comments
 (0)