syncKeyboardState method

Future<void> syncKeyboardState()

Query the engine and update _pressedKeys accordingly to the engine answer. Both the framework and the engine maintain a state of the current pressed keys. There are edge cases, related to startup and restart, where the framework needs to resynchronize its keyboard state.

Implementation

//
/// Both the framework and the engine maintain a state of the current pressed
/// keys. There are edge cases, related to startup and restart, where the framework
/// needs to resynchronize its keyboard state.
Future<void> syncKeyboardState() async {
  final Map<int, int>? keyboardState = await SystemChannels.keyboard.invokeMapMethod<int, int>(
    'getKeyboardState',
  );
  if (keyboardState != null) {
    for (final int key in keyboardState.keys) {
      final PhysicalKeyboardKey physicalKey = PhysicalKeyboardKey(key);
      final LogicalKeyboardKey logicalKey = LogicalKeyboardKey(keyboardState[key]!);
      _pressedKeys[physicalKey] = logicalKey;
    }
  }
}