getKeyData method

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

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

Implementation

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

  key = _getKeySynonym(key);

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

  assert(key.debugName != null);
  final int keyCode = platform == 'macos' || platform == 'web' ? -1 : _getKeyCode(key, platform);
  assert(platform == 'macos' || platform == 'web' || keyCode != null, 'Key $key not found in $platform keyCode map');
  final int scanCode = platform == 'web' ? -1 : _getScanCode(physicalKey, platform);
  assert(platform == 'web' || scanCode != null, 'Physical key for $key not found in $platform scanCode map');

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

  switch (platform) {
    case 'android':
      result['keyCode'] = keyCode;
      result['codePoint'] = key.keyLabel?.codeUnitAt(0);
      result['scanCode'] = scanCode;
      result['metaState'] = _getAndroidModifierFlags(key, isDown);
      break;
    case 'fuchsia':
      result['hidUsage'] = physicalKey?.usbHidUsage ?? (key.keyId & LogicalKeyboardKey.hidPlane != 0 ? key.keyId & LogicalKeyboardKey.valueMask : null);
      result['codePoint'] = key.keyLabel?.codeUnitAt(0);
      result['modifiers'] = _getFuchsiaModifierFlags(key, isDown);
      break;
    case 'linux':
      result['toolkit'] = 'glfw';
      result['keyCode'] = keyCode;
      result['scanCode'] = scanCode;
      result['modifiers'] = _getGlfwModifierFlags(key, isDown);
      break;
    case 'macos':
      result['keyCode'] = scanCode;
      result['characters'] = key.keyLabel;
      result['charactersIgnoringModifiers'] = key.keyLabel;
      result['modifiers'] = _getMacOsModifierFlags(key, isDown);
      break;
    case 'web':
      result['code'] = _getWebKeyCode(key);
      result['key'] = '';
      result['metaState'] = _getWebModifierFlags(key, isDown);
  }
  return result;
}