Repository: flutter/flutter
File: packages/flutter/lib/src/services/hardware_keyboard.dart (Dart)

Code before:
  /// which side of the keyboard it is on.
  ///
  /// Use [isLogicalKeyPressed] if you need to know which meta key was pressed.
  bool get isMetaPressed {
    return isLogicalKeyPressed(LogicalKeyboardKey.metaLeft) ||
        isLogicalKeyPressed(LogicalKeyboardKey.metaRight);
  }

  // Returns whether the event should be processed.
  //
  // Generally, this function returns true if the event is consistent with the
  // current state of the keyboard. There are exceptions when the event is
  // inconsistent but the state should be still updated according to the event.
  //
  // This function will also print debug messages if the event is inconsistent
  // with the current state.
  bool _eventShouldProceed(KeyEvent event) {
    if (event is KeyDownEvent) {
      final bool shouldProcess = !_pressedKeys.containsKey(event.physicalKey);
      assert(() {
        if (!shouldProcess) {
          _keyboardDebug(
            () =>
                'Received ${event.runtimeType} for key that is already pressed:\n'
                'Event: $event\n'
                'Pressed logical key: ${_pressedKeys[event.physicalKey]}',
          );
        }
        return true;
      }());
      return shouldProcess;
    } else if (event is KeyRepeatEvent || event is KeyUpEvent) {
      // Only checks physical key here and don't reject based on logical key,
      // since otherwise the key will never be released.
      final bool shouldProcess = _pressedKeys.containsKey(event.physicalKey);
      assert(() {
        if (!shouldProcess) {
          _keyboardDebug(
            () =>
                'Received ${event.runtimeType} for key that is not pressed:\n'
                'Event: $event',
          );
        } else if (_pressedKeys[event.physicalKey] != event.logicalKey) {
          _keyboardDebug(
            () =>
                'Received ${event.runtimeType} for key with mismatched logical key:\n'
                'Event: $event\n'
                'Pressed logical key: ${_pressedKeys[event.physicalKey]}',
          );
        }
        return true;

Review comment (bug):
The previous error message had suggested reporting the bug to Flutter, or if it's a simulated event, making sure it follows Flutter's event model as documented by `HardwareKeyboard`. Should we keep that helpful message? It makes the debug log a bit more actionable even if app developers ignore it.

Code after:
  /// which side of the keyboard it is on.
  ///
  /// Use [isLogicalKeyPressed] if you need to know which meta key was pressed.
  bool get isMetaPressed {
    return isLogicalKeyPressed(LogicalKeyboardKey.metaLeft) ||
        isLogicalKeyPressed(LogicalKeyboardKey.metaRight);
  }

  // Print debug messages if the event is inconsistent
  // with the current state, and if [debugPrintKeyboardEvents] is true.
  void _logEventIfIrregular(KeyEvent event) {
    assert(() {
      const common =
          'This is typically either due to https://github.com/flutter/flutter/issues/125975, '
          "or a bug in the embedding's key event conciliation logic.";
      if (event is KeyDownEvent) {
        if (_pressedKeys.containsKey(event.physicalKey)) {
          _keyboardDebug(
            () =>
                'ERROR: Received unexpected ${event.runtimeType} for key that is already pressed.\n'
                '$common\n'
                '    Event: $event\n'
                '    Pressed logical key: ${_pressedKeys[event.physicalKey]}',
          );
        }
      } else if (event is KeyRepeatEvent || event is KeyUpEvent) {
        if (!_pressedKeys.containsKey(event.physicalKey)) {
          _keyboardDebug(
            () =>
                'ERROR: Received unexpected ${event.runtimeType} for key that is not pressed:\n'
                '$common\n'
                '    Event: $event',
          );
        } else if (_pressedKeys[event.physicalKey] != event.logicalKey) {
          _keyboardDebug(
            () =>
                'ERROR: Received unexpected ${event.runtimeType} for key with mismatched logical key:\n'
                '$common\n'
                '    Event: $event\n'
                '    Pressed logical key: ${_pressedKeys[event.physicalKey]}',
          );
        }
      } else {
        assert(false, 'Received unexpected key event class ${event.runtimeType}');
      }
      return true;
    }());
  }

  List<KeyEventCallback> _handlers = <KeyEventCallback>[];
  bool _duringDispatch = false;