handleRawKeyEvent method Null safety

bool handleRawKeyEvent(
  1. RawKeyEvent event
)

Process a new RawKeyEvent by recording the state changes and dispatching to listeners.

Implementation

bool handleRawKeyEvent(RawKeyEvent event) {
  bool shouldDispatch = true;
  if (event is RawKeyDownEvent) {
    if (event.data.shouldDispatchEvent()) {
      _keysPressed[event.physicalKey] = event.logicalKey;
    } else {
      shouldDispatch = false;
      _hiddenKeysPressed.add(event.physicalKey);
    }
  } else if (event is RawKeyUpEvent) {
    if (!_hiddenKeysPressed.contains(event.physicalKey)) {
      // Use the physical key in the key up event to find the physical key from
      // the corresponding key down event and remove it, even if the logical
      // keys don't match.
      _keysPressed.remove(event.physicalKey);
    } else {
      _hiddenKeysPressed.remove(event.physicalKey);
      shouldDispatch = false;
    }
  }
  if (!shouldDispatch) {
    return true;
  }
  // Make sure that the modifiers reflect reality, in case a modifier key was
  // pressed/released while the app didn't have focus.
  _synchronizeModifiers(event);
  assert(
    event is! RawKeyDownEvent || _keysPressed.isNotEmpty,
    'Attempted to send a key down event when no keys are in keysPressed. '
    "This state can occur if the key event being sent doesn't properly "
    'set its modifier flags. This was the event: $event and its data: '
    '${event.data}',
  );
  // Send the event to passive listeners.
  for (final ValueChanged<RawKeyEvent> listener in List<ValueChanged<RawKeyEvent>>.from(_listeners)) {
    if (_listeners.contains(listener)) {
      listener(event);
    }
  }

  return false;
}