Skip to content

Commit e08f6e4

Browse files
Report hardware keyboard keys in Android TextInput onKeyPress
ReactEditText.onKeyDown reports every key event the view receives, from a hardware keyboard or forwarded by an input method through InputConnection.sendKeyEvent, by its KeyboardEvent.key value, with altKey, ctrlKey, metaKey and shiftKey. A Ctrl or Meta shortcut is named by its key, and so is an Alt shortcut where Alt types nothing on the layout. A key that types a character after a dead key is reported as the composed character, and system keys such as Back and volume are not reported. ReactEditTextInputConnectionWrapper reports only the text an input method types, so no key is reported twice. The key table follows the UI Events key values as Firefox for Android pairs them with key codes, so View onKeyDown also names Home, End, the function keys and Ctrl shortcuts instead of reporting them as Unidentified.
1 parent 8093e40 commit e08f6e4

12 files changed

Lines changed: 742 additions & 68 deletions

File tree

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,22 @@ export type AndroidTextInputNativeProps = Readonly<{
437437
* This will be called with `{ nativeEvent: { key: keyValue } }`
438438
* where `keyValue` is `'Enter'` or `'Backspace'` for respective keys and
439439
* the typed-in character otherwise including `' '` for space.
440+
* On Android, keys pressed on a hardware keyboard are reported as well, each
441+
* by its `KeyboardEvent.key` value (such as `'ArrowLeft'`, `'Shift'` or
442+
* `'F1'`), with `altKey`, `ctrlKey`, `metaKey` and `shiftKey` set for the
443+
* modifier keys held.
440444
* Fires before `onChange` callbacks.
441445
*/
442-
onKeyPress?: ?BubblingEventHandler<Readonly<{target: Int32, key: string}>>,
446+
onKeyPress?: ?BubblingEventHandler<
447+
Readonly<{
448+
target: Int32,
449+
key: string,
450+
altKey: boolean,
451+
ctrlKey: boolean,
452+
metaKey: boolean,
453+
shiftKey: boolean,
454+
}>,
455+
>,
443456

444457
/**
445458
* Invoked on content scroll with `{ nativeEvent: { contentOffset: { x, y } } }`.

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ type TextInputKeyPressEventData = Readonly<{
101101
key: string,
102102
target?: ?number,
103103
eventCount: number,
104+
altKey?: boolean,
105+
ctrlKey?: boolean,
106+
metaKey?: boolean,
107+
shiftKey?: boolean,
104108
...
105109
}>;
106110

@@ -850,6 +854,10 @@ type TextInputBaseProps = Readonly<{
850854
* This will be called with `{ nativeEvent: { key: keyValue } }`
851855
* where `keyValue` is `'Enter'` or `'Backspace'` for respective keys and
852856
* the typed-in character otherwise including `' '` for space.
857+
* On Android, keys pressed on a hardware keyboard are reported as well, each
858+
* by its `KeyboardEvent.key` value (such as `'ArrowLeft'`, `'Shift'` or
859+
* `'F1'`), with `altKey`, `ctrlKey`, `metaKey` and `shiftKey` set for the
860+
* modifier keys held.
853861
* Fires before `onChange` callbacks.
854862
*/
855863
onKeyPress?: ?(e: TextInputKeyPressEvent) => unknown,

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

Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal abstract class KeyEvent(
1919

2020
// Extract all needed data from keyEvent immediately to avoid storing the AndroidKeyEvent itself
2121
private val keyCode: Int = keyEvent.keyCode
22-
private val unicodeChar: Int = keyEvent.unicodeChar
22+
private val key: String = KeyValue.of(keyEvent)
2323
private val isAltPressed: Boolean = keyEvent.isAltPressed
2424
private val isCtrlPressed: Boolean = keyEvent.isCtrlPressed
2525
private val isMetaPressed: Boolean = keyEvent.isMetaPressed
@@ -35,7 +35,7 @@ internal abstract class KeyEvent(
3535
eventData.putInt("target", viewTag)
3636

3737
// W3C KeyboardEvent properties
38-
eventData.putString("key", getKeyString())
38+
eventData.putString("key", key)
3939
eventData.putString("code", getCodeString())
4040

4141
// Modifier keys
@@ -50,13 +50,6 @@ internal abstract class KeyEvent(
5050
return eventData
5151
}
5252

53-
private fun getKeyString(): String {
54-
return when {
55-
unicodeChar != 0 && !Character.isISOControl(unicodeChar) -> unicodeChar.toChar().toString()
56-
else -> KEY_NAME_MAP[keyCode] ?: UNIDENTIFIED
57-
}
58-
}
59-
6053
private fun getCodeString(): String {
6154
return CODE_MAP[keyCode] ?: UNIDENTIFIED
6255
}
@@ -128,29 +121,5 @@ internal abstract class KeyEvent(
128121
AndroidKeyEvent.KEYCODE_DPAD_CENTER to "Enter",
129122
)
130123
}
131-
132-
private val KEY_NAME_MAP: Map<Int, String> by
133-
lazy(LazyThreadSafetyMode.PUBLICATION) {
134-
mapOf(
135-
AndroidKeyEvent.KEYCODE_ENTER to "Enter",
136-
AndroidKeyEvent.KEYCODE_DPAD_CENTER to "Enter",
137-
AndroidKeyEvent.KEYCODE_SPACE to " ",
138-
AndroidKeyEvent.KEYCODE_TAB to "Tab",
139-
AndroidKeyEvent.KEYCODE_DEL to "Backspace",
140-
AndroidKeyEvent.KEYCODE_ESCAPE to "Escape",
141-
AndroidKeyEvent.KEYCODE_SHIFT_LEFT to "Shift",
142-
AndroidKeyEvent.KEYCODE_SHIFT_RIGHT to "Shift",
143-
AndroidKeyEvent.KEYCODE_CTRL_LEFT to "Control",
144-
AndroidKeyEvent.KEYCODE_CTRL_RIGHT to "Control",
145-
AndroidKeyEvent.KEYCODE_ALT_LEFT to "Alt",
146-
AndroidKeyEvent.KEYCODE_ALT_RIGHT to "Alt",
147-
AndroidKeyEvent.KEYCODE_META_LEFT to "Meta",
148-
AndroidKeyEvent.KEYCODE_META_RIGHT to "Meta",
149-
AndroidKeyEvent.KEYCODE_DPAD_UP to "ArrowUp",
150-
AndroidKeyEvent.KEYCODE_DPAD_DOWN to "ArrowDown",
151-
AndroidKeyEvent.KEYCODE_DPAD_LEFT to "ArrowLeft",
152-
AndroidKeyEvent.KEYCODE_DPAD_RIGHT to "ArrowRight",
153-
)
154-
}
155124
}
156125
}
Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
package com.facebook.react.uimanager.events
9+
10+
import android.view.KeyCharacterMap
11+
import android.view.KeyEvent as AndroidKeyEvent
12+
13+
/**
14+
* The [UI Events key value](https://www.w3.org/TR/uievents-key/) of an Android key event, with key
15+
* codes paired to key values as Firefox for Android pairs them
16+
* ([NativeKeyToDOMKeyName.h](https://searchfox.org/mozilla-central/source/widget/NativeKeyToDOMKeyName.h)).
17+
*/
18+
internal object KeyValue {
19+
20+
fun of(event: AndroidKeyEvent): String = of(event.keyCode, typedCharacter(event))
21+
22+
/** The key value of the key [keyCode] when it types [character], as [typedCharacter] reads it. */
23+
fun of(keyCode: Int, character: Int): String =
24+
when {
25+
isDeadKey(character) -> DEAD
26+
character != 0 && !Character.isISOControl(character) -> String(Character.toChars(character))
27+
else -> NAMED_KEYS[keyCode] ?: UNIDENTIFIED
28+
}
29+
30+
/**
31+
* The character [event] types with Control and Meta released, and with Alt released too when Alt
32+
* types nothing on the layout, because the key value of a shortcut such as Ctrl+Z or Alt+E is the
33+
* character of its key.
34+
*/
35+
fun typedCharacter(event: AndroidKeyEvent): Int {
36+
val character = event.getUnicodeChar(event.metaState and SHORTCUT_MODIFIERS.inv())
37+
return if (character != 0 || !event.isAltPressed) {
38+
character
39+
} else {
40+
event.getUnicodeChar(event.metaState and (SHORTCUT_MODIFIERS or ALT_MODIFIERS).inv())
41+
}
42+
}
43+
44+
fun isDeadKey(character: Int): Boolean = (character and KeyCharacterMap.COMBINING_ACCENT) != 0
45+
46+
private const val SHORTCUT_MODIFIERS: Int =
47+
AndroidKeyEvent.META_CTRL_MASK or AndroidKeyEvent.META_META_MASK
48+
private const val ALT_MODIFIERS: Int = AndroidKeyEvent.META_ALT_MASK
49+
private const val DEAD: String = "Dead"
50+
private const val UNIDENTIFIED: String = "Unidentified"
51+
52+
private val NAMED_KEYS: Map<Int, String> by
53+
lazy(LazyThreadSafetyMode.PUBLICATION) {
54+
mapOf(
55+
AndroidKeyEvent.KEYCODE_ALT_LEFT to "Alt",
56+
AndroidKeyEvent.KEYCODE_ALT_RIGHT to "Alt",
57+
AndroidKeyEvent.KEYCODE_CAPS_LOCK to "CapsLock",
58+
AndroidKeyEvent.KEYCODE_CTRL_LEFT to "Control",
59+
AndroidKeyEvent.KEYCODE_CTRL_RIGHT to "Control",
60+
AndroidKeyEvent.KEYCODE_FUNCTION to "Fn",
61+
AndroidKeyEvent.KEYCODE_META_LEFT to "Meta",
62+
AndroidKeyEvent.KEYCODE_META_RIGHT to "Meta",
63+
AndroidKeyEvent.KEYCODE_NUM_LOCK to "NumLock",
64+
AndroidKeyEvent.KEYCODE_SCROLL_LOCK to "ScrollLock",
65+
AndroidKeyEvent.KEYCODE_SHIFT_LEFT to "Shift",
66+
AndroidKeyEvent.KEYCODE_SHIFT_RIGHT to "Shift",
67+
AndroidKeyEvent.KEYCODE_SYM to "Symbol",
68+
AndroidKeyEvent.KEYCODE_DPAD_CENTER to "Enter",
69+
AndroidKeyEvent.KEYCODE_ENTER to "Enter",
70+
AndroidKeyEvent.KEYCODE_NUMPAD_ENTER to "Enter",
71+
AndroidKeyEvent.KEYCODE_TAB to "Tab",
72+
AndroidKeyEvent.KEYCODE_DPAD_DOWN to "ArrowDown",
73+
AndroidKeyEvent.KEYCODE_DPAD_LEFT to "ArrowLeft",
74+
AndroidKeyEvent.KEYCODE_DPAD_RIGHT to "ArrowRight",
75+
AndroidKeyEvent.KEYCODE_DPAD_UP to "ArrowUp",
76+
AndroidKeyEvent.KEYCODE_MOVE_END to "End",
77+
AndroidKeyEvent.KEYCODE_MOVE_HOME to "Home",
78+
AndroidKeyEvent.KEYCODE_PAGE_DOWN to "PageDown",
79+
AndroidKeyEvent.KEYCODE_PAGE_UP to "PageUp",
80+
AndroidKeyEvent.KEYCODE_DEL to "Backspace",
81+
AndroidKeyEvent.KEYCODE_CLEAR to "Clear",
82+
AndroidKeyEvent.KEYCODE_COPY to "Copy",
83+
AndroidKeyEvent.KEYCODE_CUT to "Cut",
84+
AndroidKeyEvent.KEYCODE_FORWARD_DEL to "Delete",
85+
AndroidKeyEvent.KEYCODE_INSERT to "Insert",
86+
AndroidKeyEvent.KEYCODE_PASTE to "Paste",
87+
AndroidKeyEvent.KEYCODE_MENU to "ContextMenu",
88+
AndroidKeyEvent.KEYCODE_ESCAPE to "Escape",
89+
AndroidKeyEvent.KEYCODE_HELP to "Help",
90+
AndroidKeyEvent.KEYCODE_BREAK to "Pause",
91+
AndroidKeyEvent.KEYCODE_ZOOM_IN to "ZoomIn",
92+
AndroidKeyEvent.KEYCODE_ZOOM_OUT to "ZoomOut",
93+
AndroidKeyEvent.KEYCODE_BRIGHTNESS_DOWN to "BrightnessDown",
94+
AndroidKeyEvent.KEYCODE_BRIGHTNESS_UP to "BrightnessUp",
95+
AndroidKeyEvent.KEYCODE_MEDIA_EJECT to "Eject",
96+
AndroidKeyEvent.KEYCODE_POWER to "Power",
97+
AndroidKeyEvent.KEYCODE_SYSRQ to "PrintScreen",
98+
AndroidKeyEvent.KEYCODE_SLEEP to "Standby",
99+
AndroidKeyEvent.KEYCODE_WAKEUP to "WakeUp",
100+
AndroidKeyEvent.KEYCODE_HENKAN to "Convert",
101+
AndroidKeyEvent.KEYCODE_LANGUAGE_SWITCH to "GroupNext",
102+
AndroidKeyEvent.KEYCODE_SWITCH_CHARSET to "ModeChange",
103+
AndroidKeyEvent.KEYCODE_MUHENKAN to "NonConvert",
104+
AndroidKeyEvent.KEYCODE_EISU to "Eisu",
105+
AndroidKeyEvent.KEYCODE_KATAKANA_HIRAGANA to "HiraganaKatakana",
106+
AndroidKeyEvent.KEYCODE_KANA to "KanjiMode",
107+
AndroidKeyEvent.KEYCODE_ZENKAKU_HANKAKU to "ZenkakuHankaku",
108+
AndroidKeyEvent.KEYCODE_F1 to "F1",
109+
AndroidKeyEvent.KEYCODE_F2 to "F2",
110+
AndroidKeyEvent.KEYCODE_F3 to "F3",
111+
AndroidKeyEvent.KEYCODE_F4 to "F4",
112+
AndroidKeyEvent.KEYCODE_F5 to "F5",
113+
AndroidKeyEvent.KEYCODE_F6 to "F6",
114+
AndroidKeyEvent.KEYCODE_F7 to "F7",
115+
AndroidKeyEvent.KEYCODE_F8 to "F8",
116+
AndroidKeyEvent.KEYCODE_F9 to "F9",
117+
AndroidKeyEvent.KEYCODE_F10 to "F10",
118+
AndroidKeyEvent.KEYCODE_F11 to "F11",
119+
AndroidKeyEvent.KEYCODE_F12 to "F12",
120+
AndroidKeyEvent.KEYCODE_CHANNEL_DOWN to "ChannelDown",
121+
AndroidKeyEvent.KEYCODE_CHANNEL_UP to "ChannelUp",
122+
AndroidKeyEvent.KEYCODE_MEDIA_FAST_FORWARD to "MediaFastForward",
123+
AndroidKeyEvent.KEYCODE_MEDIA_PAUSE to "MediaPause",
124+
AndroidKeyEvent.KEYCODE_MEDIA_PLAY to "MediaPlay",
125+
AndroidKeyEvent.KEYCODE_MEDIA_PLAY_PAUSE to "MediaPlayPause",
126+
AndroidKeyEvent.KEYCODE_MEDIA_RECORD to "MediaRecord",
127+
AndroidKeyEvent.KEYCODE_MEDIA_REWIND to "MediaRewind",
128+
AndroidKeyEvent.KEYCODE_MEDIA_STOP to "MediaStop",
129+
AndroidKeyEvent.KEYCODE_MEDIA_NEXT to "MediaTrackNext",
130+
AndroidKeyEvent.KEYCODE_MEDIA_PREVIOUS to "MediaTrackPrevious",
131+
AndroidKeyEvent.KEYCODE_VOLUME_DOWN to "AudioVolumeDown",
132+
AndroidKeyEvent.KEYCODE_VOLUME_UP to "AudioVolumeUp",
133+
AndroidKeyEvent.KEYCODE_VOLUME_MUTE to "AudioVolumeMute",
134+
AndroidKeyEvent.KEYCODE_MUTE to "MicrophoneVolumeMute",
135+
AndroidKeyEvent.KEYCODE_CALCULATOR to "LaunchCalculator",
136+
AndroidKeyEvent.KEYCODE_CALENDAR to "LaunchCalendar",
137+
AndroidKeyEvent.KEYCODE_CONTACTS to "LaunchContacts",
138+
AndroidKeyEvent.KEYCODE_ENVELOPE to "LaunchMail",
139+
AndroidKeyEvent.KEYCODE_MUSIC to "LaunchMusicPlayer",
140+
AndroidKeyEvent.KEYCODE_EXPLORER to "LaunchWebBrowser",
141+
AndroidKeyEvent.KEYCODE_BOOKMARK to "BrowserFavorites",
142+
AndroidKeyEvent.KEYCODE_FORWARD to "BrowserForward",
143+
AndroidKeyEvent.KEYCODE_SEARCH to "BrowserSearch",
144+
AndroidKeyEvent.KEYCODE_APP_SWITCH to "AppSwitch",
145+
AndroidKeyEvent.KEYCODE_CALL to "Call",
146+
AndroidKeyEvent.KEYCODE_CAMERA to "Camera",
147+
AndroidKeyEvent.KEYCODE_FOCUS to "CameraFocus",
148+
AndroidKeyEvent.KEYCODE_ENDCALL to "EndCall",
149+
AndroidKeyEvent.KEYCODE_BACK to "GoBack",
150+
AndroidKeyEvent.KEYCODE_HOME to "GoHome",
151+
AndroidKeyEvent.KEYCODE_HEADSETHOOK to "HeadsetHook",
152+
AndroidKeyEvent.KEYCODE_NOTIFICATION to "Notification",
153+
AndroidKeyEvent.KEYCODE_MANNER_MODE to "MannerMode",
154+
AndroidKeyEvent.KEYCODE_TV to "TV",
155+
AndroidKeyEvent.KEYCODE_3D_MODE to "TV3DMode",
156+
AndroidKeyEvent.KEYCODE_TV_ANTENNA_CABLE to "TVAntennaCable",
157+
AndroidKeyEvent.KEYCODE_TV_AUDIO_DESCRIPTION to "TVAudioDescription",
158+
AndroidKeyEvent.KEYCODE_TV_AUDIO_DESCRIPTION_MIX_DOWN to "TVAudioDescriptionMixDown",
159+
AndroidKeyEvent.KEYCODE_TV_AUDIO_DESCRIPTION_MIX_UP to "TVAudioDescriptionMixUp",
160+
AndroidKeyEvent.KEYCODE_TV_CONTENTS_MENU to "TVContentsMenu",
161+
AndroidKeyEvent.KEYCODE_TV_DATA_SERVICE to "TVDataService",
162+
AndroidKeyEvent.KEYCODE_TV_INPUT to "TVInput",
163+
AndroidKeyEvent.KEYCODE_TV_INPUT_COMPONENT_1 to "TVInputComponent1",
164+
AndroidKeyEvent.KEYCODE_TV_INPUT_COMPONENT_2 to "TVInputComponent2",
165+
AndroidKeyEvent.KEYCODE_TV_INPUT_COMPOSITE_1 to "TVInputComposite1",
166+
AndroidKeyEvent.KEYCODE_TV_INPUT_COMPOSITE_2 to "TVInputComposite2",
167+
AndroidKeyEvent.KEYCODE_TV_INPUT_HDMI_1 to "TVInputHDMI1",
168+
AndroidKeyEvent.KEYCODE_TV_INPUT_HDMI_2 to "TVInputHDMI2",
169+
AndroidKeyEvent.KEYCODE_TV_INPUT_HDMI_3 to "TVInputHDMI3",
170+
AndroidKeyEvent.KEYCODE_TV_INPUT_HDMI_4 to "TVInputHDMI4",
171+
AndroidKeyEvent.KEYCODE_TV_INPUT_VGA_1 to "TVInputVGA1",
172+
AndroidKeyEvent.KEYCODE_TV_NETWORK to "TVNetwork",
173+
AndroidKeyEvent.KEYCODE_TV_NUMBER_ENTRY to "TVNumberEntry",
174+
AndroidKeyEvent.KEYCODE_TV_POWER to "TVPower",
175+
AndroidKeyEvent.KEYCODE_TV_RADIO_SERVICE to "TVRadioService",
176+
AndroidKeyEvent.KEYCODE_TV_SATELLITE to "TVSatellite",
177+
AndroidKeyEvent.KEYCODE_TV_SATELLITE_BS to "TVSatelliteBS",
178+
AndroidKeyEvent.KEYCODE_TV_SATELLITE_CS to "TVSatelliteCS",
179+
AndroidKeyEvent.KEYCODE_TV_SATELLITE_SERVICE to "TVSatelliteToggle",
180+
AndroidKeyEvent.KEYCODE_TV_TERRESTRIAL_ANALOG to "TVTerrestrialAnalog",
181+
AndroidKeyEvent.KEYCODE_TV_TERRESTRIAL_DIGITAL to "TVTerrestrialDigital",
182+
AndroidKeyEvent.KEYCODE_TV_TIMER_PROGRAMMING to "TVTimer",
183+
AndroidKeyEvent.KEYCODE_AVR_INPUT to "AVRInput",
184+
AndroidKeyEvent.KEYCODE_AVR_POWER to "AVRPower",
185+
AndroidKeyEvent.KEYCODE_PROG_RED to "ColorF0Red",
186+
AndroidKeyEvent.KEYCODE_PROG_GREEN to "ColorF1Green",
187+
AndroidKeyEvent.KEYCODE_PROG_YELLOW to "ColorF2Yellow",
188+
AndroidKeyEvent.KEYCODE_PROG_BLUE to "ColorF3Blue",
189+
AndroidKeyEvent.KEYCODE_CAPTIONS to "ClosedCaptionToggle",
190+
AndroidKeyEvent.KEYCODE_DVR to "DVR",
191+
AndroidKeyEvent.KEYCODE_GUIDE to "Guide",
192+
AndroidKeyEvent.KEYCODE_INFO to "Info",
193+
AndroidKeyEvent.KEYCODE_MEDIA_AUDIO_TRACK to "MediaAudioTrack",
194+
AndroidKeyEvent.KEYCODE_LAST_CHANNEL to "MediaLast",
195+
AndroidKeyEvent.KEYCODE_MEDIA_TOP_MENU to "MediaTopMenu",
196+
AndroidKeyEvent.KEYCODE_MEDIA_SKIP_BACKWARD to "MediaSkipBackward",
197+
AndroidKeyEvent.KEYCODE_MEDIA_SKIP_FORWARD to "MediaSkipForward",
198+
AndroidKeyEvent.KEYCODE_MEDIA_STEP_BACKWARD to "MediaStepBackward",
199+
AndroidKeyEvent.KEYCODE_MEDIA_STEP_FORWARD to "MediaStepForward",
200+
AndroidKeyEvent.KEYCODE_NAVIGATE_IN to "NavigateIn",
201+
AndroidKeyEvent.KEYCODE_NAVIGATE_NEXT to "NavigateNext",
202+
AndroidKeyEvent.KEYCODE_NAVIGATE_OUT to "NavigateOut",
203+
AndroidKeyEvent.KEYCODE_NAVIGATE_PREVIOUS to "NavigatePrevious",
204+
AndroidKeyEvent.KEYCODE_PAIRING to "Pairing",
205+
AndroidKeyEvent.KEYCODE_WINDOW to "PinPToggle",
206+
AndroidKeyEvent.KEYCODE_SETTINGS to "Settings",
207+
AndroidKeyEvent.KEYCODE_STB_INPUT to "STBInput",
208+
AndroidKeyEvent.KEYCODE_STB_POWER to "STBPower",
209+
AndroidKeyEvent.KEYCODE_TV_TELETEXT to "Teletext",
210+
AndroidKeyEvent.KEYCODE_TV_ZOOM_MODE to "ZoomToggle",
211+
AndroidKeyEvent.KEYCODE_SOFT_LEFT to "SoftLeft",
212+
AndroidKeyEvent.KEYCODE_SOFT_RIGHT to "SoftRight",
213+
)
214+
}
215+
}

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import com.facebook.react.uimanager.ReactAccessibilityDelegate
7575
import com.facebook.react.uimanager.StateWrapper
7676
import com.facebook.react.uimanager.UIManagerHelper
7777
import com.facebook.react.uimanager.events.EventDispatcher
78+
import com.facebook.react.uimanager.events.KeyValue
7879
import com.facebook.react.uimanager.style.BorderRadiusProp
7980
import com.facebook.react.uimanager.style.BorderStyle
8081
import com.facebook.react.uimanager.style.LogicalEdge
@@ -145,6 +146,7 @@ public open class ReactEditText public constructor(context: Context) : AppCompat
145146
private var touchDownY = 0f
146147
private val touchSlop = ViewConfiguration.get(context).scaledTouchSlop
147148
private var onKeyPress = false
149+
private var isDeadKeyPending = false
148150
private val textAttributes: TextAttributes
149151
private var typefaceDirty = false
150152
private var fontFamily: String? = null
@@ -383,6 +385,34 @@ public open class ReactEditText public constructor(context: Context) : AppCompat
383385
}
384386
}
385387

388+
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
389+
val character = KeyValue.typedCharacter(event)
390+
if (onKeyPress && !event.isSystem) {
391+
eventDispatcher?.dispatchEvent(
392+
ReactTextInputKeyPressEvent(
393+
UIManagerHelper.getSurfaceId(this),
394+
id,
395+
KeyValue.of(keyCode, composeWithPendingDeadKey(character)),
396+
event.metaState,
397+
)
398+
)
399+
}
400+
val handled = super.onKeyDown(keyCode, event)
401+
isDeadKeyPending =
402+
KeyValue.isDeadKey(character) || (isDeadKeyPending && KeyEvent.isModifierKey(keyCode))
403+
return handled
404+
}
405+
406+
// QwertyKeyListener leaves a dead key's accent selected and composes it with the next character.
407+
private fun composeWithPendingDeadKey(character: Int): Int {
408+
val text = text
409+
if (!isDeadKeyPending || character == 0 || text == null || selectionEnd - selectionStart != 1) {
410+
return character
411+
}
412+
val composed = KeyEvent.getDeadChar(text[selectionStart].code, character)
413+
return if (composed != 0) composed else character
414+
}
415+
386416
// Consume 'Enter' key events: TextView tries to give focus to the next TextInput, but it can't
387417
// since we only allow JS to change focus, which in turn causes TextView to crash.
388418
override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean {
@@ -552,6 +582,7 @@ public open class ReactEditText public constructor(context: Context) : AppCompat
552582
FLog.e(TAG, "onSelectionChanged[$id]: $selStart $selEnd")
553583
}
554584

585+
isDeadKeyPending = false
555586
super.onSelectionChanged(selStart, selEnd)
556587
if (selectionWatcher != null && hasFocus()) {
557588
selectionWatcher?.onSelectionChanged(selStart, selEnd)

0 commit comments

Comments
 (0)