Skip to content

[Windows] Bogus keys reported in RawKeyboard.instance.keysPressed #76857

Description

@tgucio

This is similar to #75627 but this time for Windows with easy reproducible steps.

The cause seems to be in the same logic in RawKeyboard that expects that every KeyDown event is followed by a KeyUp event which doesn't seem to be the case on multiple platforms.

Since the current EditableText implementation relies on RawKeyboard.instance.keysPressed, when this issue is hit, all keyboard navigation (plus backspace etc.) stops working forever and ever (or until the "bad" key, the Windows logo in this case, is pressed again which clears its status in keysPressed).

Steps to Reproduce

  1. Run flutter create editable_text_keys.
  2. Update main.dart as follows:
import 'package:flutter/services.dart';
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'EditableText keys test',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  TextEditingController _textController;
  FocusNode _focusNode;

  @override
  void initState() {
    super.initState();
    _textController = TextEditingController(text: 'some\nmulti\nline\ntext\n\njust\na few\nlines\nmore\n...\n...\n');
    _focusNode = FocusNode(
      onKey: (_, RawKeyEvent keyEvent) {
        if (keyEvent is! RawKeyDownEvent) return false;
        final Set<LogicalKeyboardKey> keysPressed = LogicalKeyboardKey.collapseSynonyms(RawKeyboard.instance.keysPressed);
        print('FocusNode onKey RawKeyEvent: $keyEvent | keysPressed: $keysPressed');
        return false;
      }
    );
  }

  @override
  void dispose() {
    _focusNode.dispose();
    _textController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('EditableText keys test')),
      body: Container(
        margin: EdgeInsets.all(10.0),
        child: EditableText(
          controller: _textController,
          focusNode: _focusNode,
          maxLines: null,
          autofocus: true,
          style: TextStyle(fontSize: 24.0, color: Colors.black),
          cursorColor: Colors.amber,
          backgroundCursorColor: Colors.grey,
        ),
      ),
    );
  }
}
  1. Run the application on Windows 10. Press different keys, including arrow keys and observe in the printed logs if the RawKeyEvent and keysPressed match what's being pressed.
  2. Make sure 2 or more keyboard layouts are enabled. Press the default keyboard layout switch shortcut (Windows logo key + space) and press it again so it switches to another layout
  3. Switch back to the Flutter app window and repeat step 3.

Expected results: Only the actual keys being pressed are reported in the RawKeyboard.instance.keysPressed set.

flutter: FocusNode onKey RawKeyEvent: RawKeyDownEvent#570d4(logicalKey: LogicalKeyboardKey#70052(keyId: "0x100070052", keyLabel: "", debugName: "Arrow Up"), physicalKey: PhysicalKeyboardKey#70060(usbHidUsage: "0x00070060", debugName: "Numpad 8")) | keysPressed: {LogicalKeyboardKey#70052(keyId: "0x100070052", keyLabel: "", debugName: "Arrow Up")}
flutter: FocusNode onKey RawKeyEvent: RawKeyDownEvent#dc135(logicalKey: LogicalKeyboardKey#70051(keyId: "0x100070051", keyLabel: "", debugName: "Arrow Down"), physicalKey: PhysicalKeyboardKey#7005a(usbHidUsage: "0x0007005a", debugName: "Numpad 2")) | keysPressed: {LogicalKeyboardKey#70051(keyId: "0x100070051", keyLabel: "", debugName: "Arrow Down")}
flutter: FocusNode onKey RawKeyEvent: RawKeyDownEvent#b82a9(logicalKey: LogicalKeyboardKey#7004f(keyId: "0x10007004f", keyLabel: "", debugName: "Arrow Right"), physicalKey: PhysicalKeyboardKey#7005e(usbHidUsage: "0x0007005e", debugName: "Numpad 6")) | keysPressed: {LogicalKeyboardKey#7004f(keyId: "0x10007004f", keyLabel: "", debugName: "Arrow Right")}
flutter: FocusNode onKey RawKeyEvent: RawKeyDownEvent#02e55(logicalKey: LogicalKeyboardKey#70052(keyId: "0x100070052", keyLabel: "", debugName: "Arrow Up"), physicalKey: PhysicalKeyboardKey#70060(usbHidUsage: "0x00070060", debugName: "Numpad 8")) | keysPressed: {LogicalKeyboardKey#70052(keyId: "0x100070052", keyLabel: "", debugName: "Arrow Up")}

Actual results: On any key press, The key "Meta" is constantly reported in RawKeyboard.instance.keysPressed despite not being pressed:

flutter: FocusNode onKey RawKeyEvent: RawKeyDownEvent#55ec0(logicalKey: LogicalKeyboardKey#70051(keyId: "0x100070051", keyLabel: "", debugName: "Arrow Down"), physicalKey: PhysicalKeyboardKey#7005a(usbHidUsage: "0x0007005a", debugName: "Numpad 2")) | keysPressed: {LogicalKeyboardKey#700e3(keyId: "0x201000700e3", keyLabel: "", debugName: "Meta"), LogicalKeyboardKey#70051(keyId: "0x100070051", keyLabel: "", debugName: "Arrow Down")}
flutter: FocusNode onKey RawKeyEvent: RawKeyDownEvent#5dcd1(logicalKey: LogicalKeyboardKey#70052(keyId: "0x100070052", keyLabel: "", debugName: "Arrow Up"), physicalKey: PhysicalKeyboardKey#70060(usbHidUsage: "0x00070060", debugName: "Numpad 8")) | keysPressed: {LogicalKeyboardKey#700e3(keyId: "0x201000700e3", keyLabel: "", debugName: "Meta"), LogicalKeyboardKey#70052(keyId: "0x100070052", keyLabel: "", debugName: "Arrow Up")}
flutter: FocusNode onKey RawKeyEvent: RawKeyDownEvent#5a1c3(logicalKey: LogicalKeyboardKey#70051(keyId: "0x100070051", keyLabel: "", debugName: "Arrow Down"), physicalKey: PhysicalKeyboardKey#7005a(usbHidUsage: "0x0007005a", debugName: "Numpad 2")) | keysPressed: {LogicalKeyboardKey#700e3(keyId: "0x201000700e3", keyLabel: "", debugName: "Meta"), LogicalKeyboardKey#70051(keyId: "0x100070051", keyLabel: "", debugName: "Arrow Down")}
flutter: FocusNode onKey RawKeyEvent: RawKeyDownEvent#d822d(logicalKey: LogicalKeyboardKey#70052(keyId: "0x100070052", keyLabel: "", debugName: "Arrow Up"), physicalKey: PhysicalKeyboardKey#70060(usbHidUsage: "0x00070060", debugName: "Numpad 8")) | keysPressed: {LogicalKeyboardKey#700e3(keyId: "0x201000700e3", keyLabel: "", debugName: "Meta"), LogicalKeyboardKey#70052(keyId: "0x100070052", keyLabel: "", debugName: "Arrow Up")}
Logs
PS > flutter analyze
Analyzing editable_text_keys...
No issues found! (ran in 4.4s)
PS > flutter doctor -v
[√] Flutter (Channel dev, 1.27.0-8.0.pre, on Microsoft Windows [Version 10.0.19042.804], locale en-US)
    • Flutter version 1.27.0-8.0.pre at C:\flutter
    • Framework revision b7d4806243 (7 days ago), 2021-02-19 09:22:45 -0800
    • Engine revision 6993cb229b
    • Dart version 2.13.0 (build 2.13.0-30.0.dev)

[X] Android toolchain - develop for Android devices
    X Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/docs/get-started/install/windows#android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.


[√] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.8.4)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
    • Visual Studio Community 2019 version 16.8.30907.101
    • Windows 10 SDK version 10.0.18362.0

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/windows#android-setup for detailed instructions).

[√] VS Code, 64-bit edition (version 1.53.2)
    • VS Code at C:\Program Files\Microsoft VS Code
    • Flutter extension version 3.19.0

[√] Connected device (2 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.19042.804]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 88.0.4324.150

! Doctor found issues in 2 categories.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work lista: desktopRunning on desktopa: text inputEntering text in a text field or keyboard related problemsengineflutter/engine related. See also e: labels.found in release: 1.27Found to occur in 1.27has reproducible stepsThe issue has been confirmed reproducible and is ready to work onplatform-windowsBuilding on or for Windows specificallyr: fixedIssue is closed as already fixed in a newer version

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions