toSpec method

String toSpec()

Converts this KeyEvent to a specification string.

This is the inverse of KeyEvent.fromString. Outputs generic modifier names in canonical order: ctrl+alt+shift+super+hyper+meta

Does not encode: eventType, baseLayoutKey, keyPad, capsLock

Examples:

  • KeyEvent.fromString('backSpace').toSpec()'backSpace'
  • KeyEvent.fromString('ctrl+a').toSpec()'ctrl+a'
  • KeyEvent(KeyCode.named(KeyCodeName.leftCtrl)).toSpec()'ctrl'

Implementation

String toSpec() {
  final parts = <String>[];
  var keySpec = _keyToSpec(code);

  for (final (modifier, name) in _modifierSpec) {
    // Skip modifier if it matches the key (e.g., leftCtrl key → 'ctrl')
    if (name == keySpec) continue;
    if (modifiers.has(modifier)) {
      parts.add(name);
    }
  }

  // Lowercase single uppercase letters when shift is present
  if (modifiers.has(KeyModifiers.shift) && keySpec.length == 1) {
    final code = keySpec.codeUnitAt(0);
    // Check if it's an uppercase letter (A-Z)
    if (code >= 0x41 && code <= 0x5A) {
      keySpec = keySpec.toLowerCase();
    }
  }

  parts.add(keySpec);
  return parts.join('+');
}