ChemPal Documentation - v0.0.13-beta.5
    Preparing search index...

    Function parseBinding

    • Parses a key-binding string such as "mod+shift+r" into a ParsedBinding. The final +-separated token is treated as the key; earlier tokens are modifiers. The mod token expands to meta on macOS and ctrl elsewhere.

      Parameters

      • binding: string

        The key-binding string. Tokens are case-insensitive.

      Returns ParsedBinding

      The parsed binding, ready for matching against a KeyboardEvent.

      parseBinding("mod+shift+r");
      // => on macOS: { meta: true, ctrl: false, alt: false, shift: true, key: "r" }
      // => on other: { meta: false, ctrl: true, alt: false, shift: true, key: "r" }
      export function parseBinding(binding: string): ParsedBinding {
      const tokens = binding
      .split("+")
      .map((t) => t.trim().toLowerCase())
      .filter(Boolean);

      const parsed: ParsedBinding = {
      meta: false,
      ctrl: false,
      alt: false,
      shift: false,
      key: "",
      };

      const mac = isMac();
      const last = tokens.pop() ?? "";

      for (const token of tokens) {
      if (token === "mod") {
      if (mac) parsed.meta = true;
      else parsed.ctrl = true;
      continue;
      }
      const flag = MOD_ALIASES[token];
      if (flag) parsed[flag] = true;
      }

      parsed.key = last;
      return parsed;
      }