From 63dd50788d4a9acab6b397a39094dcd1e2d2568f Mon Sep 17 00:00:00 2001 From: Juanjo Tugores Date: Fri, 12 Jul 2024 10:54:55 -0700 Subject: [PATCH] Relax the event assumptions for keydown events. (#53830) This PR addresses an issue where autocompleting a text field, even without direct keyboard input, unexpectedly triggers keydown events. To resolve this, the code now relaxes the casting assumptions to accommodate a wider range of event types, not just keyboard events. By just adding the following script to the console, and filling the text field using autocomplete, you can see that indeed the fired event is not of type `KeyboardEvent` but `Event`. ```javascript document.body.addEventListener('keydown', console.log) ``` Fixes https://github.com/flutter/flutter/issues/149968 *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].* [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../src/engine/platform_dispatcher/view_focus_binding.dart | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/web_ui/lib/src/engine/platform_dispatcher/view_focus_binding.dart b/lib/web_ui/lib/src/engine/platform_dispatcher/view_focus_binding.dart index 31acaf7ca9273..5c1eb85c31241 100644 --- a/lib/web_ui/lib/src/engine/platform_dispatcher/view_focus_binding.dart +++ b/lib/web_ui/lib/src/engine/platform_dispatcher/view_focus_binding.dart @@ -69,8 +69,10 @@ final class ViewFocusBinding { }); late final DomEventListener _handleKeyDown = createDomEventListener((DomEvent event) { - event as DomKeyboardEvent; - if (event.shiftKey ?? false) { + // The right event type needs to be checked because Chrome seems to be firing + // `Event` events instead of `KeyboardEvent` events when autofilling is used. + // See https://github.com/flutter/flutter/issues/149968 for more info. + if (event is DomKeyboardEvent && (event.shiftKey ?? false)) { _viewFocusDirection = ui.ViewFocusDirection.backward; } });