mirror of
https://github.com/redphx/better-xcloud.git
synced 2025-08-08 06:08:27 +02:00
6.0
This commit is contained in:
113
src/modules/mkb/key-helper.ts
Normal file → Executable file
113
src/modules/mkb/key-helper.ts
Normal file → Executable file
@@ -1,8 +1,36 @@
|
||||
import { MouseButtonCode, WheelCode } from "@enums/mkb";
|
||||
import { MouseButtonCode, WheelCode, type KeyCode } from "@/enums/mkb";
|
||||
|
||||
export const enum KeyModifier {
|
||||
CTRL = 1,
|
||||
SHIFT = 2,
|
||||
ALT = 4,
|
||||
};
|
||||
|
||||
export type KeyEventInfo = {
|
||||
code: KeyCode | MouseButtonCode | WheelCode;
|
||||
modifiers?: number;
|
||||
};
|
||||
|
||||
export class KeyHelper {
|
||||
static #NON_PRINTABLE_KEYS = {
|
||||
'Backquote': '`',
|
||||
private static readonly NON_PRINTABLE_KEYS = {
|
||||
Backquote: '`',
|
||||
Minus: '-',
|
||||
Equal: '=',
|
||||
BracketLeft: '[',
|
||||
BracketRight: ']',
|
||||
Backslash: '\\',
|
||||
Semicolon: ';',
|
||||
Quote: '\'',
|
||||
Comma: ',',
|
||||
Period: '.',
|
||||
Slash: '/',
|
||||
|
||||
NumpadMultiply: 'Numpad *',
|
||||
NumpadAdd: 'Numpad +',
|
||||
NumpadSubtract: 'Numpad -',
|
||||
NumpadDecimal: 'Numpad .',
|
||||
NumpadDivide: 'Numpad /',
|
||||
NumpadEqual: 'Numpad =',
|
||||
|
||||
// Mouse buttons
|
||||
[MouseButtonCode.LEFT_CLICK]: 'Left Click',
|
||||
@@ -15,12 +43,19 @@ export class KeyHelper {
|
||||
[WheelCode.SCROLL_RIGHT]: 'Scroll Right',
|
||||
};
|
||||
|
||||
static getKeyFromEvent(e: Event) {
|
||||
let code;
|
||||
let name;
|
||||
static getKeyFromEvent(e: Event): KeyEventInfo | null {
|
||||
let code: KeyEventInfo['code'] | null = null;
|
||||
let modifiers;
|
||||
|
||||
if (e instanceof KeyboardEvent) {
|
||||
code = e.code || e.key;
|
||||
code = (e.code || e.key) as KeyCode;
|
||||
|
||||
// Modifiers
|
||||
modifiers = 0;
|
||||
modifiers ^= e.ctrlKey ? KeyModifier.CTRL : 0;
|
||||
modifiers ^= e.shiftKey ? KeyModifier.SHIFT : 0;
|
||||
modifiers ^= e.altKey ? KeyModifier.ALT : 0;
|
||||
|
||||
} else if (e instanceof WheelEvent) {
|
||||
if (e.deltaY < 0) {
|
||||
code = WheelCode.SCROLL_UP;
|
||||
@@ -32,20 +67,47 @@ export class KeyHelper {
|
||||
code = WheelCode.SCROLL_RIGHT;
|
||||
}
|
||||
} else if (e instanceof MouseEvent) {
|
||||
code = 'Mouse' + e.button;
|
||||
code = 'Mouse' + e.button as MouseButtonCode;
|
||||
}
|
||||
|
||||
if (code) {
|
||||
name = KeyHelper.codeToKeyName(code);
|
||||
const results: KeyEventInfo = { code };
|
||||
if (modifiers) {
|
||||
results.modifiers = modifiers;
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
return code ? {code, name} : null;
|
||||
return null;
|
||||
}
|
||||
|
||||
static codeToKeyName(code: string) {
|
||||
return (
|
||||
// @ts-ignore
|
||||
KeyHelper.#NON_PRINTABLE_KEYS[code]
|
||||
static getFullKeyCodeFromEvent(e: KeyboardEvent): string {
|
||||
const key = KeyHelper.getKeyFromEvent(e);
|
||||
return key ? `${key.code}:${key.modifiers || 0}` : '';
|
||||
}
|
||||
|
||||
static parseFullKeyCode(str: string | undefined | null): KeyEventInfo | null {
|
||||
if (!str) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const tmp = str.split(':');
|
||||
|
||||
const code = tmp[0] as KeyEventInfo['code'];
|
||||
const modifiers = parseInt(tmp[1]);
|
||||
|
||||
return {
|
||||
code,
|
||||
modifiers,
|
||||
} as KeyEventInfo;
|
||||
}
|
||||
|
||||
static codeToKeyName(key: KeyEventInfo): string {
|
||||
const { code, modifiers } = key;
|
||||
|
||||
const text = [(
|
||||
KeyHelper.NON_PRINTABLE_KEYS[code as keyof typeof KeyHelper.NON_PRINTABLE_KEYS]
|
||||
||
|
||||
(code.startsWith('Key') && code.substring(3))
|
||||
||
|
||||
@@ -62,6 +124,27 @@ export class KeyHelper {
|
||||
(code.endsWith('Right') && ('Right ' + code.replace('Right', '')))
|
||||
||
|
||||
code
|
||||
);
|
||||
)];
|
||||
|
||||
if (modifiers && modifiers !== 0) {
|
||||
if (!code.startsWith('Control') && !code.startsWith('Shift') && !code.startsWith('Alt')) {
|
||||
// Shift
|
||||
if (modifiers & KeyModifier.SHIFT) {
|
||||
text.unshift('Shift');
|
||||
}
|
||||
|
||||
// Alt
|
||||
if (modifiers & KeyModifier.ALT) {
|
||||
text.unshift('Alt');
|
||||
}
|
||||
|
||||
// Ctrl
|
||||
if (modifiers & KeyModifier.CTRL) {
|
||||
text.unshift('Ctrl');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return text.join(' + ');
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user