61 lines
1.3 KiB
TypeScript
61 lines
1.3 KiB
TypeScript
export default class Key {
|
|
public readonly key: string;
|
|
|
|
public readonly shift: boolean;
|
|
|
|
public readonly ctrl: boolean;
|
|
|
|
public readonly alt: boolean;
|
|
|
|
public readonly meta: boolean;
|
|
|
|
constructor({ key, shift, ctrl, alt, meta }: {
|
|
key: string;
|
|
shift: boolean;
|
|
ctrl: boolean;
|
|
alt: boolean;
|
|
meta: boolean;
|
|
}) {
|
|
this.key = key;
|
|
this.shift = shift;
|
|
this.ctrl = ctrl;
|
|
this.alt = alt;
|
|
this.meta = meta;
|
|
}
|
|
|
|
static fromMapKey(str: string): Key {
|
|
if (str.startsWith('<') && str.endsWith('>')) {
|
|
let inner = str.slice(1, -1);
|
|
let shift = inner.includes('S-');
|
|
let base = inner.slice(inner.lastIndexOf('-') + 1);
|
|
if (shift && base.length === 1) {
|
|
base = base.toUpperCase();
|
|
} else if (!shift && base.length === 1) {
|
|
base = base.toLowerCase();
|
|
}
|
|
return new Key({
|
|
key: base,
|
|
shift: shift,
|
|
ctrl: inner.includes('C-'),
|
|
alt: inner.includes('A-'),
|
|
meta: inner.includes('M-'),
|
|
});
|
|
}
|
|
|
|
return new Key({
|
|
key: str,
|
|
shift: str.toLowerCase() !== str,
|
|
ctrl: false,
|
|
alt: false,
|
|
meta: false,
|
|
});
|
|
}
|
|
|
|
equals(key: Key) {
|
|
return this.key === key.key &&
|
|
this.ctrl === key.ctrl &&
|
|
this.meta === key.meta &&
|
|
this.alt === key.alt &&
|
|
this.shift === key.shift;
|
|
}
|
|
}
|