Repository: flutter
Issue: Pressing and releasing a key before the framework has started throws
Labels: framework,engine,has reproducible steps,P2,found in release: 3.7,found in release: 3.10,team-engine,triaged-engine

### Is there an existing issue for this?

- [X] I have searched the [existing issues](https://github.com/flutter/flutter/issues)
- [X] I have read the [guide to filing a bug](https://flutter.dev/docs/resources/bug-reports)

### Steps to reproduce

1. Launch the code sample (tried on Linux and on Android).
2. Do a hot restart and very quickly focused the Flutter app, press and release a key (this might need several tries, because the key has to be pressed and released in the short time where the engine is ready to listen to platform keyboard events and the framework is not fully initialized).

### Expected results

No error

### Actual results

Error thrown: `flutter: A KeyUpEvent is dispatched, but the state shows that the physical key is not pressed.`

### Code sample

<details><summary>Code sample</summary>

```dart

import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
        body: Center(
          child: TextField(
          ),
        ),
      ),
    );
  }
}
```

</details>


### Screenshots or Video

<details>
<summary>Screenshots / Video demonstration</summary>

[Upload media here]

</details>


### Logs

<details><summary>Logs</summary>

```console
flutter: ══╡ EXCEPTION CAUGHT BY SERVICES LIBRARY ╞══════════════════════════════════════════════════════════
flutter: The following assertion was thrown during a platform message callback:
flutter: A KeyUpEvent is dispatched, but the state shows that the physical key is not pressed. If this occurs
flutter: in real application, please report this bug to Flutter. If this occurs in unit tests, please ensure
flutter: that simulated events follow Flutter's event model as documented in `HardwareKeyboard`. This was the
flutter: event: KeyUpEvent#17c68(physicalKey: PhysicalKeyboardKey#a8a55(usbHidUsage: "0x00070014", debugName:
flutter: "Key Q"), logicalKey: LogicalKeyboardKey#12bb1(keyId: "0x00000061", keyLabel: "A", debugName: "Key
flutter: A"), character: null, timeStamp: 5:07:41.188000)
flutter: 'package:flutter/src/services/hardware_keyboard.dart':
flutter: Failed assertion: line 471 pos 16: '_pressedKeys.containsKey(event.physicalKey)'
flutter:
flutter: Either the assertion indicates an error in the framework itself, or we should provide substantially
flutter: more information in this error message to help you determine and fix the underlying cause.
flutter: In either case, please report this assertion by filing a bug on GitHub:
flutter:   https://github.com/flutter/flutter/issues/new?template=2_bug.yml
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #2      HardwareKeyboard._assertEventIsRegular.<anonymous closure> (package:flutter/src/services/hardware_keyboard.dart:471:16)
flutter: #3      HardwareKeyboard._assertEventIsRegular (package:flutter/src/services/hardware_keyboard.dart:482:6)
flutter: #4      HardwareKeyboard.handleKeyEvent (package:flutter/src/services/hardware_keyboard.dart:601:5)
flutter: #5      KeyEventManager.handleRawKeyMessage (package:flutter/src/services/hardware_keyboard.dart:1021:37)
flutter: #6      BasicMessageChannel.setMessageHandler.<anonymous closure> (package:flutter/src/services/platform_channel.dart:212:49)
flutter: #7      _DefaultBinaryMessenger.setMessageHandler.<anonymous closure> (package:flutter/src/services/binding.dart:481:35)
flutter: #8      _invoke2 (dart:ui/hooks.dart:174:13)
flutter: #9      _ChannelCallbackRecord.invoke (dart:ui/channel_buffers.dart:40:5)
flutter: #10     _Channel._drainStep (dart:ui/channel_buffers.dart:223:31)
flutter: (elided 4 frames from class _AssertionError and dart:async)
flutter: ════════════════════════════════════════════════════════════════════════════════════════════════════```

</details>


### Flutter Doctor output

<details><summary>Doctor output</summary>

```console
[✓] Flutter (Channel master, 3.10.0-3.0.pre.403, on Ubuntu 22.10
    5.19.0-41-generic, locale fr_FR.UTF-8)
    • Flutter version 3.10.0-3.0.pre.403 on channel master at
      /home/bruno/Nevercode/flutter
    • Upstream repository git@github.com:NevercodeHQ/flutter.git
    • FLUTTER_GIT_URL = git@github.com:NevercodeHQ/flutter.git
    • Framework revision 110fe75b5d (il y a 7 heures), 2023-05-03 02:11:35 -0400
    • Engine revision 0407a16a9b
    • Dart version 3.1.0 (build 3.1.0-66.0.dev)
    • DevTools version 2.23.1

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
    • Android SDK at /home/bruno/Android/Sdk
    • Platform android-33, build-tools 33.0.1
    • Java binary at: /home/bruno/Produits/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build
      11.0.13+0-b1751.21-8125866)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • Chrome at google-chrome

[✓] Linux toolchain - develop for Linux desktop
    • Ubuntu clang version 15.0.7
    • cmake version 3.24.2
    • ninja version 1.11.0
    • pkg-config version 0.29.2

[✓] Android Studio (version 2021.3)
    • Android Studio at /home/bruno/Produits/android-studio
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build
      11.0.13+0-b1751.21-8125866)

[✓] VS Code
    • VS Code at /snap/code/current
    • Flutter extension version 3.62.0

[✓] Connected device (2 available)
    • Linux (desktop) • linux  • linux-x64      • Ubuntu 22.10 5.19.0-41-generic
    • Chrome (web)    • chrome • web-javascript • Google Chrome 112.0.5615.49

[✓] Network resources
    • All expected network resources are available.

• No issues found!
```

</details>