Skip to content

Commit d631c94

Browse files
bumjungfacebook-github-bot
authored andcommitted
Include the native event timestamp in onHWKeyEvent on Android
Summary: `onHWKeyEvent` tells JS which key was pressed but not when it was pressed, so a listener can only time a press from the moment the device event reaches the JavaScript thread. That delivery is asynchronous, so any latency measured from JS silently excludes the native-to-JS hop — and excludes more of it the busier the JS thread is, which is exactly when the interaction is slowest. Add the originating `KeyEvent.getEventTime()` to the event payload. It is `SystemClock.uptimeMillis()`, the same `CLOCK_MONOTONIC` base that `performance.now()` reads in JS, so a listener can subtract the two directly with no clock conversion. The field is omitted for the focus and blur events, which have no originating hardware event. Additive and behaviour-preserving: no existing payload key changes, and nothing in the framework reads the new one. ## Changelog Changelog: [Android][Added] - Add `eventTime` to the `onHWKeyEvent` device event payload Reviewed By: rozele Differential Revision: D120451212
1 parent af4d8eb commit d631c94

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/ReactAndroidHWInputDeviceHelper.kt‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ internal class ReactAndroidHWInputDeviceHelper {
2929
(eventKeyAction == KeyEvent.ACTION_UP || eventKeyAction == KeyEvent.ACTION_DOWN) &&
3030
KEY_EVENTS_ACTIONS.containsKey(eventKeyCode)
3131
) {
32-
dispatchEvent(context, KEY_EVENTS_ACTIONS[eventKeyCode], lastFocusedViewId, eventKeyAction)
32+
dispatchEvent(
33+
context,
34+
KEY_EVENTS_ACTIONS[eventKeyCode],
35+
lastFocusedViewId,
36+
eventKeyAction,
37+
ev.eventTime,
38+
)
3339
}
3440
}
3541

@@ -58,11 +64,15 @@ internal class ReactAndroidHWInputDeviceHelper {
5864
eventType: String?,
5965
targetViewId: Int,
6066
eventKeyAction: Int = -1,
67+
eventTime: Long = NO_EVENT_TIME,
6168
) {
6269
val event: WritableMap =
6370
WritableNativeMap().apply {
6471
putString("eventType", eventType)
6572
putInt("eventKeyAction", eventKeyAction)
73+
if (eventTime != NO_EVENT_TIME) {
74+
putDouble("eventTime", eventTime.toDouble())
75+
}
6676
if (targetViewId != View.NO_ID) {
6777
putInt("tag", targetViewId)
6878
}
@@ -71,6 +81,8 @@ internal class ReactAndroidHWInputDeviceHelper {
7181
}
7282

7383
private companion object {
84+
private const val NO_EVENT_TIME: Long = -1
85+
7486
/**
7587
* Contains a mapping between handled KeyEvents and the corresponding navigation event that
7688
* should be fired when the KeyEvent is received.

0 commit comments

Comments
 (0)