Relax the event assumptions for keydown events. (flutter/engine#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
This commit is contained in:
Juanjo Tugores
2024-07-12 10:54:55 -07:00
committed by GitHub
parent 15becdc41e
commit 93de0249f9

View File

@@ -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;
}
});