Conversation
…place deprecated keyCode.
| } | ||
|
|
||
| export interface KeyMap { | ||
| key: string[]; // Using array to support multiple key variations (e.g. 'a' and 'A') |
There was a problem hiding this comment.
When using keyCode, both a and A share the same code (65), but since we're switching to using the key property, a and A are treat as distinct values. Therefore, we should use an array to store all variations of the same key.
…d definitions for consistency
| export interface KeyMap { | ||
| key: string[]; // Using array to support multiple key variations (e.g. 'a' and 'A') | ||
| keyCode: number; | ||
| } |
There was a problem hiding this comment.
Instead of propagate the change to outside of this file, we should keep KeyMap inside this file and keep the previous code that works with keyCode (number) unchanged.
/**
* An enum for key codes.
*/
export enum KeyCodes {
ESC = 27,
SHIFT = 16,
ENTER = 13,
BACKSPACE = 8,
DELETE = 46,
ARROW_LEFT = 37,
ARROW_UP = 38,
ARROW_RIGHT = 39,
ARROW_DOWN = 40,
A = 65,
C = 67,
D = 68,
L = 76,
R = 82,
T = 84,
V = 86,
X = 88,
Z = 90,
}
export type KeyCode =
KeyCodes.ESC
| KeyCodes.SHIFT
| KeyCodes.ENTER
| KeyCodes.BACKSPACE
| KeyCodes.DELETE
| KeyCodes.ARROW_LEFT
| KeyCodes.ARROW_UP
| KeyCodes.ARROW_RIGHT
| KeyCodes.ARROW_DOWN
| KeyCodes.A
| KeyCodes.C
| KeyCodes.D
| KeyCodes.L
| KeyCodes.R
| KeyCodes.T
| KeyCodes.V
| KeyCodes.X
| KeyCodes.Z;
const KeyToKeyCode: { [key: string]: KeyCode } = {
Escape: KeyCodes.ESC,
Shift: KeyCodes.SHIFT,
Enter: KeyCodes.ENTER,
Backspace: KeyCodes.BACKSPACE,
Delete: KeyCodes.DELETE,
ArrowLeft: KeyCodes.ARROW_LEFT,
ArrowUp: KeyCodes.ARROW_UP,
ArrowRight: KeyCodes.ARROW_RIGHT,
ArrowDown: KeyCodes.ARROW_DOWN,
a: KeyCodes.A,
A: KeyCodes.A,
c: KeyCodes.C,
C: KeyCodes.C,
d: KeyCodes.D,
D: KeyCodes.D,
l: KeyCodes.L,
L: KeyCodes.L,
r: KeyCodes.R,
R: KeyCodes.R,
t: KeyCodes.T,
T: KeyCodes.T,
v: KeyCodes.V,
V: KeyCodes.V,
x: KeyCodes.X,
X: KeyCodes.X,
z: KeyCodes.Z,
Z: KeyCodes.Z,
};
export function getKeyCodeFromEvent(e: KeyboardEvent): KeyCode | undefined {
return KeyToKeyCode[e.key];
}Besides, KeyMap should not be used as a key for a map in KeyCodeToKeyCommandMap (the previous type of the key of KeyCodeToKeyCommandMap (Key) is wrong).
There was a problem hiding this comment.
Don't worry to change export class Key to something else that is more appropriate like enum. I didn't know much about TypeScript when I started converting Kotlin into it (and Copilot at that time was not really good too)
There was a problem hiding this comment.
I see, that's a nice suggestion, let me try to apply this.
This pull request updates the key command handling to replace the deprecated
event.keyCode. The changes include:Keyclass to useKeyMapobjects for better key event handling.getKeyFromEventfunction to map keyboard events toKeyMapobjects.KeyCommandControllerand related classes to use the new key mapping.KeyCommandsto useKeyMapinstead of key codes directly.These improvements enhance the maintainability and extendability of the key command handling mechanism while replacing the deprecated
event.keyCode.