Keybinding.parse constructor

Keybinding.parse(
  1. String command
)

Implementation

factory Keybinding.parse(String command) {
  command = command.toLowerCase().trim();

  var isAltPressed = false;
  var isControlPressed = false;
  var isMetaPressed = false;
  var isShiftPressed = false;

  var matchedModifier = false;

  do {
    matchedModifier = false;
    if (RegExp(r'^alt(\+|\-)').hasMatch(command)) {
      isAltPressed = true;
      command = command.substring(4); // 4 = 'alt '.length
      matchedModifier = true;
    }
    if (RegExp(r'^ctrl(\+|\-)').hasMatch(command)) {
      isControlPressed = true;
      command = command.substring(5); // 5 = 'ctrl '.length
      matchedModifier = true;
    }
    if (RegExp(r'^shift(\+|\-)').hasMatch(command)) {
      isShiftPressed = true;
      command = command.substring(6); // 6 = 'shift '.length
      matchedModifier = true;
    }
    if (RegExp(r'^meta(\+|\-)').hasMatch(command)) {
      isMetaPressed = true;
      command = command.substring(5); // 5 = 'meta '.length
      matchedModifier = true;
    }
    if (RegExp(r'^cmd(\+|\-)').hasMatch(command) ||
        RegExp(r'^win(\+|\-)').hasMatch(command)) {
      isMetaPressed = true;
      command = command.substring(4); // 4 = 'win '.length
      matchedModifier = true;
    }
  } while (matchedModifier);

  return Keybinding(
    isAltPressed: isAltPressed,
    isControlPressed: isControlPressed,
    isMetaPressed: isMetaPressed,
    isShiftPressed: isShiftPressed,
    keyLabel: command,
  );
}