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

Code before:
  // 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;
      }());
      return shouldProcess;
    } else {
      assert(() {
        _keyboardDebug(
          () =>
              'Received unknown KeyEvent subtype:\n'
              'Event: $event',

Review comment (bug):
When I first saw this "don't reject based on logical key" comment, I incorrectly thought this meant "don't check if the map `_pressedKeys` has a key entry for the logical key". I didn't realize what this instead meant was "don't check if the map's _value_ matches the logical key".

I'd consider moving the second half of this comment down to line 544. Alternatively, consider adding more context to this command. Maybe something like:

```suggestion
      // Reject repeat or up events if the physical isn't known to be pressed.
      // However, do process these events even if the logical key is mismatched -
      // this ensures the key is released or updated to the latest logical key value.
```

Code after:
  // 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;
  List<KeyEventCallback>? _modifiedHandlers;

  /// Register a listener that is called every time a hardware key event
  /// occurs.
  ///
  /// All registered handlers will be invoked in order regardless of
  /// their return value. The return value indicates whether Flutter
  /// "handles" the event. If any handler returns true, the event