k5kit
Version:
Utilities for TypeScript and Svelte
30 lines (29 loc) • 1.12 kB
JavaScript
export function is_mac() {
return /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform);
}
/** Check the pressed modifiers for an event. This is strict, so if you pass no modifiers, it will return true if the event has no modifiers. */
export function check_modifiers(e, ...modifiers) {
let shift = false;
let alt = false;
let ctrl = false;
let meta = false;
for (const modifier of modifiers) {
if (modifier === 'shift') {
shift = true;
}
else if (modifier === 'alt') {
alt = true;
}
else if (modifier === 'cmdOrCtrl') {
ctrl = !is_mac();
meta = !ctrl;
}
}
return (!!e.shiftKey === shift && !!e.altKey === alt && !!e.ctrlKey === ctrl && !!e.metaKey === meta);
}
/** Check the pressed key and modifiers for a keyboard event. This is strict, so if you pass no modifiers, it will return true if the event has no modifiers. */
export function check_shortcut(e, key, ...modifiers) {
if (e.key.toUpperCase() !== key.toUpperCase())
return false;
return check_modifiers(e, ...modifiers);
}