示例工程 34-global-shortcuts-autostart
34-global-shortcuts-autostart/demo-01-accelerator-policy/accelerator.ts
type Modifier = "CommandOrControl" | "Command" | "Control" | "Alt" | "Shift";
type Parsed =
| { ok: true; accelerator: string; key: string }
| { ok: false; reason: string };
const MODS: Record<string, Modifier> = {
commandorcontrol: "CommandOrControl",
cmdorctrl: "CommandOrControl",
command: "Command",
cmd: "Command",
meta: "Command",
control: "Control",
ctrl: "Control",
alt: "Alt",
option: "Alt",
shift: "Shift",
};
const ORDER: Modifier[] = [
"CommandOrControl",
"Command",
"Control",
"Alt",
"Shift",
];
const NAMED = new Set([
"space",
"tab",
"escape",
"esc",
"enter",
"return",
"backspace",
"delete",
"del",
"up",
"down",
"left",
"right",
"pageup",
"pagedown",
"home",
"end",
]);
function normalizeKey(token: string): string | null {
const lower = token.toLowerCase();
const aliases: Record<string, string> = {
esc: "Escape",
return: "Enter",
del: "Delete",
pageup: "PageUp",
pagedown: "PageDown",
};
if (aliases[lower]) return aliases[lower];
if (/^[a-z]$/.test(lower)) return lower.toUpperCase();
if (/^[0-9]$/.test(lower)) return lower;
if (/^f([1-9]|1[0-9]|2[0-4])$/.test(lower)) return `F${lower.slice(1)}`;
if (NAMED.has(lower)) {
return lower.charAt(0).toUpperCase() + lower.slice(1);
}
return null;
}
function parse(input: string): Parsed {
const text = input.trim();
if (!text) return { ok: false, reason: "EMPTY" };
const tokens = text
.split("+")
.map((token) => token.trim())
.filter((token, index, all) => !(token === "" && index === all.length - 1));
if (tokens.some((token) => token.length === 0)) {
return { ok: false, reason: "UNKNOWN_KEY" };
}
const mods: Modifier[] = [];
let key: string | null = null;
for (const token of tokens) {
const mod = MODS[token.toLowerCase()];
if (mod) {
if (mods.includes(mod))
return { ok: false, reason: "DUPLICATE_MODIFIER" };
mods.push(mod);
continue;
}
const next = normalizeKey(token);
if (!next) return { ok: false, reason: "UNKNOWN_KEY" };
if (key) return { ok: false, reason: "EXTRA_KEY" };
key = next;
}
if (!key) return { ok: false, reason: "MISSING_KEY" };
if (mods.length === 0) return { ok: false, reason: "MISSING_MODIFIER" };
if (
mods.includes("CommandOrControl") &&
(mods.includes("Command") || mods.includes("Control"))
) {
return { ok: false, reason: "MODIFIER_CONFLICT" };
}
return {
ok: true,
accelerator: [...ORDER.filter((mod) => mods.includes(mod)), key].join("+"),
key,
};
}
type Verdict = "OK" | "INVALID" | "ALREADY_OWNED" | "CONFLICT";
function classify(input: {
threw: boolean;
owned: boolean;
returned: boolean;
}): Verdict {
if (input.threw) return "INVALID";
if (input.owned) return "ALREADY_OWNED";
if (!input.returned) return "CONFLICT";
return "OK";
}
const cases = [
parse("CmdOrCtrl+Shift+Space"),
parse("cmd+shift+k"),
parse("Ctrl+"),
parse("Ctrl+K+J"),
parse("Hyper+K"),
parse("Ctrl+Ctrl+K"),
parse("Shift+F5"),
parse("CommandOrControl+Command+K"),
];
const verdicts = [
classify({ threw: false, owned: false, returned: true }),
classify({ threw: false, owned: true, returned: false }),
classify({ threw: false, owned: false, returned: false }),
classify({ threw: true, owned: false, returned: false }),
];
const [a, b, c, d, e, f, g, h] = cases;
const ok =
a?.ok === true &&
a.accelerator === "CommandOrControl+Shift+Space" &&
b?.ok === true &&
b.accelerator === "Command+Shift+K" &&
c?.ok === false &&
c.reason === "MISSING_KEY" &&
d?.ok === false &&
d.reason === "EXTRA_KEY" &&
e?.ok === false &&
e.reason === "UNKNOWN_KEY" &&
f?.ok === false &&
f.reason === "DUPLICATE_MODIFIER" &&
g?.ok === true &&
g.accelerator === "Shift+F5" &&
h?.ok === false &&
h.reason === "MODIFIER_CONFLICT" &&
verdicts.join(",") === "OK,ALREADY_OWNED,CONFLICT,INVALID";
console.log(JSON.stringify({ evt: "smoke", ok, cases, verdicts }));