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>);
  // The following `handleRawKeyEvent` will call `_convertRawEventAndStore`
  // unless the event is not dispatched.
  bool 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}');
  }

  if (keyMessageHandler != null) {
    handled = keyMessageHandler!(KeyMessage(_keyEventsSinceLastMessage, rawEvent)) || handled;
  }
  _keyEventsSinceLastMessage.clear();

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