getKeyData static method

Map<String, dynamic> getKeyData(
  1. LogicalKeyboardKey key, {
  2. required String platform,
  3. bool isDown = true,
  4. PhysicalKeyboardKey? physicalKey,
  5. String? character,
})

Get a raw key data map given a LogicalKeyboardKey and a platform.

Implementation

static Map<String, dynamic> getKeyData(
  LogicalKeyboardKey key, {
  required String platform,
  bool isDown = true,
  PhysicalKeyboardKey? physicalKey,
  String? character,
}) {
  assert(_osIsSupported(platform), 'Platform $platform not supported for key simulation');

  key = _getKeySynonym(key);

  // Find a suitable physical key if none was supplied.
  physicalKey ??= _findPhysicalKeyByPlatform(key, platform);

  assert(key.debugName != null);

  final Map<String, dynamic> result = <String, dynamic>{
    'type': isDown ? 'keydown' : 'keyup',
    'keymap': platform,
  };

  final String resultCharacter = character ?? _keyLabel(key) ?? '';
  void assignWeb() {
    final _WebKeyLocationPair keyLocation = _getWebKeyLocation(key, resultCharacter);
    final PhysicalKeyboardKey actualPhysicalKey = physicalKey ?? _inferPhysicalKey(key);
    result['code'] = _getWebCode(actualPhysicalKey);
    result['key'] = keyLocation.key;
    result['location'] = keyLocation.location;
    result['metaState'] = _getWebModifierFlags(key, isDown);
  }
  if (kIsWeb) {
    assignWeb();
    return result;
  }
  final int keyCode = _getKeyCode(key, platform);
  final int scanCode = _getScanCode(physicalKey, platform);

  switch (platform) {
    case 'android':
      result['keyCode'] = keyCode;
      if (resultCharacter.isNotEmpty) {
        result['codePoint'] = resultCharacter.codeUnitAt(0);
        result['character'] = resultCharacter;
      }
      result['scanCode'] = scanCode;
      result['metaState'] = _getAndroidModifierFlags(key, isDown);
    case 'fuchsia':
      result['hidUsage'] = physicalKey.usbHidUsage;
      if (resultCharacter.isNotEmpty) {
        result['codePoint'] = resultCharacter.codeUnitAt(0);
      }
      result['modifiers'] = _getFuchsiaModifierFlags(key, isDown);
    case 'linux':
      result['toolkit'] = 'glfw';
      result['keyCode'] = keyCode;
      result['scanCode'] = scanCode;
      result['modifiers'] = _getGlfwModifierFlags(key, isDown);
      result['unicodeScalarValues'] = resultCharacter.isNotEmpty ? resultCharacter.codeUnitAt(0) : 0;
    case 'macos':
      result['keyCode'] = scanCode;
      if (resultCharacter.isNotEmpty) {
        result['characters'] = resultCharacter;
        result['charactersIgnoringModifiers'] = resultCharacter;
      }
      result['modifiers'] = _getMacOsModifierFlags(key, isDown);
    case 'ios':
      result['keyCode'] = scanCode;
      result['characters'] = resultCharacter;
      result['charactersIgnoringModifiers'] = resultCharacter;
      result['modifiers'] = _getIOSModifierFlags(key, isDown);
    case 'windows':
      result['keyCode'] = keyCode;
      result['scanCode'] = scanCode;
      if (resultCharacter.isNotEmpty) {
        result['characterCodePoint'] = resultCharacter.codeUnitAt(0);
      }
      result['modifiers'] = _getWindowsModifierFlags(key, isDown);
    case 'web':
      assignWeb();
  }
  return result;
}