handleRawKeyMessage method Null safety

Future<Map<String, dynamic>> handleRawKeyMessage(
  1. dynamic message
)

Handles a raw key message.

This method is the handler to SystemChannels.keyEvent, processing the JSON form of the native key message and returns the responds for the channel.

Implementation

Future<Map<String, dynamic>> handleRawKeyMessage(dynamic message) async {
  if (_transitMode == null) {
    _transitMode = KeyDataTransitMode.rawKeyData;
    // Convert raw events using a listener so that conversion only occurs if
    // the raw event should be dispatched.
    _rawKeyboard.addListener(_convertRawEventAndStore);
  }
  final RawKeyEvent rawEvent = RawKeyEvent.fromMessage(message as Map<String, dynamic>);

  bool shouldDispatch = true;
  if (rawEvent is RawKeyDownEvent) {
    if (!rawEvent.data.shouldDispatchEvent()) {
      shouldDispatch = false;
      _skippedRawKeysPressed.add(rawEvent.physicalKey);
    } else {
      _skippedRawKeysPressed.remove(rawEvent.physicalKey);
    }
  } else if (rawEvent is RawKeyUpEvent) {
    if (_skippedRawKeysPressed.contains(rawEvent.physicalKey)) {
      _skippedRawKeysPressed.remove(rawEvent.physicalKey);
      shouldDispatch = false;
    }
  }

  bool handled = true;
  if (shouldDispatch) {
    // The following `handleRawKeyEvent` will call `_convertRawEventAndStore`
    // unless the event is not dispatched.
    handled = _rawKeyboard.handleRawKeyEvent(rawEvent);

    for (final KeyEvent event in _keyEventsSinceLastMessage) {
      handled = _hardwareKeyboard.handleKeyEvent(event) || handled;
    }
    if (_transitMode == KeyDataTransitMode.rawKeyData) {
      assert(setEquals(_rawKeyboard.physicalKeysPressed, _hardwareKeyboard.physicalKeysPressed),
        'RawKeyboard reported ${_rawKeyboard.physicalKeysPressed}, '
        'while HardwareKeyboard reported ${_hardwareKeyboard.physicalKeysPressed}');
    }

    handled = _dispatchKeyMessage(_keyEventsSinceLastMessage, rawEvent) || handled;
    _keyEventsSinceLastMessage.clear();
  }

  return <String, dynamic>{ 'handled': handled };
}