The key-binding string. Tokens are case-insensitive.
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;
}
Parses a key-binding string such as
"mod+shift+r"into aParsedBinding. The final+-separated token is treated as the key; earlier tokens are modifiers. Themodtoken expands tometaon macOS andctrlelsewhere.