Repository: flutter
Issue: [Android] Can not input anything when press alt+tab with bluetooth keyboard
Labels: a: text input,platform-android,framework,has reproducible steps,P2,found in release: 2.5,found in release: 2.6,team-android,triaged-android

<!-- Thank you for using Flutter!

     If you are looking for support, please check out our documentation
     or consider asking a question on Stack Overflow:
      * https://flutter.dev/
      * https://api.flutter.dev/
      * https://stackoverflow.com/questions/tagged/flutter?sort=frequent

     If you have found a bug or if our documentation doesn't have an answer
     to what you're looking for, then fill out the template below. Please read
     our guide to filing a bug first: https://flutter.dev/docs/resources/bug-reports
-->

## Steps to Reproduce

<!-- Please include full steps to reproduce so that we can reproduce the problem. -->

1. Execute `flutter run` on the code sample <!-- (see "Code sample" section below) -->
2. Connect to bluetooth keyboard on Android device.
3. Press alt+tab

**Expected results:** <!-- what did you want to see? -->

When back to the app, it should be able to input text.

**Actual results:** <!-- what did you see? -->

Can not input anything.

<details>
<summary>Code sample</summary>

<!--
      Please create a minimal reproducible sample that shows the problem
      and attach it below between the lines with the backticks.

      To create it you can use `flutter create bug` command and update the `main.dart` file.

      Alternatively, you can use https://dartpad.dev/
      which is capable of creating and running small Flutter apps.

      Without this we will unlikely be able to progress on the issue, and because of that
      we regretfully will have to close it.
-->

```dart
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: const KeyboardIssue(),
    );
  }
}

class KeyboardIssue extends StatelessWidget {
  const KeyboardIssue({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(body: Center(child: TextField()));
  }
}

```

</details>

<details>
  <summary>Logs</summary>

<!--
      Run your application with `flutter run --verbose` and attach all the
      log output below between the lines with the backticks. If there is an
      exception, please see if the error message includes enough information
      to explain how to solve the issue.
-->

```
[+21701 ms] D/InputMethodManager(31956): showSoftInput() view=io.flutter.embedding.android.FlutterView{a1b3a90 VFE...... .F....ID 0,0-1440,3064 #1 aid=1073741824} flags=0 reason=SHOW_SOFT_INPUT
[   +9 ms] W/IInputConnectionWrapper(31956): beginBatchEdit on inactive InputConnection
[        ] W/IInputConnectionWrapper(31956): getTextBeforeCursor on inactive InputConnection
[   +6 ms] W/IInputConnectionWrapper(31956): getTextAfterCursor on inactive InputConnection
[        ] W/IInputConnectionWrapper(31956): getSelectedText on inactive InputConnection
[        ] W/IInputConnectionWrapper(31956): endBatchEdit on inactive InputConnection
[  +40 ms] D/InsetsController(31956): show(ime(), fromIme=true)
[  +77 ms] D/InsetsController(31956): show(ime(), fromIme=true)

======== Exception caught by services library ======================================================
The following assertion was thrown during a platform message callback:
A KeyDownEvent is dispatched, but the state shows that the physical key is already pressed. If this occurs in real application, please report this bug to Flutter. If this occurs in unit tests, please ensure that simulated events follow Flutter's event model as documented in `HardwareKeyboard`. This was the event: KeyDownEvent#71bc1(physicalKey: PhysicalKeyboardKey#700e2(usbHidUsage: "0x000700e2", debugName: "Alt Left"), logicalKey: LogicalKeyboardKey#00104(keyId: "0x200000104", keyLabel: "Alt Left", debugName: "Alt Left"), character: null, timeStamp: 227:30:13.472758, synthesized)
'package:flutter/src/services/hardware_keyboard.dart':
Failed assertion: line 437 pos 16: '!_pressedKeys.containsKey(event.physicalKey)'


Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=2_bug.md

When the exception was thrown, this was the stack: 
#2      HardwareKeyboard._assertEventIsRegular.<anonymous closure> (package:flutter/src/services/hardware_keyboard.dart:437:16)
#3      HardwareKeyboard._assertEventIsRegular (package:flutter/src/services/hardware_keyboard.dart:452:6)
#4      HardwareKeyboard.handleKeyEvent (package:flutter/src/services/hardware_keyboard.dart:543:5)
#5      KeyEventManager.handleRawKeyMessage (package:flutter/src/services/hardware_keyboard.dart:821:35)
#6      BasicMessageChannel.setMessageHandler.<anonymous closure> (package:flutter/src/services/platform_channel.dart:73:49)
#7      BasicMessageChannel.setMessageHandler.<anonymous closure> (package:flutter/src/services/platform_channel.dart:72:47)
#8      _DefaultBinaryMessenger.setMessageHandler.<anonymous closure> (package:flutter/src/services/binding.dart:379:35)
#9      _DefaultBinaryMessenger.setMessageHandler.<anonymous closure> (package:flutter/src/services/binding.dart:376:46)
#10     _invoke2.<anonymous closure> (dart:ui/hooks.dart:205:15)
#14     _invoke2 (dart:ui/hooks.dart:204:10)
#15     _ChannelCallbackRecord.invoke (dart:ui/channel_buffers.dart:42:5)
#16     _Channel.push (dart:ui/channel_buffers.dart:132:31)
#17     ChannelBuffers.push (dart:ui/channel_buffers.dart:329:17)
#18     PlatformDispatcher._dispatchPlatformMessage (dart:ui/platform_dispatcher.dart:544:22)
#19     _dispatchPlatformMessage (dart:ui/hooks.dart:92:31)
(elided 5 frames from class _AssertionError and dart:async)
====================================================================================================

```

<!--
     Run `flutter analyze` and attach any output of that command below.
     If there are any analysis errors, try resolving them before filing this issue.
-->

```
```

<!-- Finally, paste the output of running `flutter doctor -v` here. -->

```
[✓] Flutter (Channel stable, 2.5.3, on Manjaro Linux 5.10.79-1-MANJARO, locale zh_CN.UTF-8)
    • Flutter version 2.5.3 at /home/darren/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 18116933e7 (7 周前), 2021-10-15 10:46:35 -0700
    • Engine revision d3ea636dc5
    • Dart version 2.14.4
    • Pub download mirror https://pub.flutter-io.cn
    • Flutter download mirror https://storage.flutter-io.cn

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /home/darren/Android/Sdk
    • Platform android-31, build-tools 31.0.0
    • ANDROID_SDK_ROOT = /home/darren/Android/Sdk
    • Java binary at: /opt/android-studio/jre/bin/java
    • Java version openjdk version "11.0.10" 2021-01-19
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • CHROME_EXECUTABLE = /usr/bin/google-chrome-stable

[✓] Linux toolchain - develop for Linux desktop
    • clang version 13.0.0
    • cmake version 3.21.4
    • ninja version 1.10.2
    • pkg-config version 1.8.0

[✓] Android Studio (version 2020.3)
    • Android Studio at /opt/android-studio
    • Flutter plugin version 62.0.1
    • Dart plugin version 203.8452
    • Java version openjdk version "11.0.10" 2021-01-19

[✓] Connected device (3 available)
    • Pixel 6 Pro (mobile) • 19051FDEE001RV • android-arm64  • Android 12 (API 31)
    • Linux (desktop)      • linux          • linux-x64      • Manjaro Linux 5.10.79-1-MANJARO
    • Chrome (web)         • chrome         • web-javascript • Google Chrome 96.0.4664.45

• No issues found!
```

</details>


<!--
      Consider also attaching screenshots and/or videos to better
      illustrate the issue.

      You can upload them directly on GitHub.
      Beware that video file size is li

https://user-images.githubusercontent.com/20706299/144163878-749c174e-37f8-41e1-91ad-d1ad700b0579.mp4

mited to 10MB.
-->