Skip to content

Virtual whiteboard for sketching hand-drawn like diagrams

fix: replaced KeyboardEvent.code with KeyboardEvent.key for all letters (#5523)

Jakob Guddas79bd3b8cdafa
diff --git a/src/actions/actionClipboard.tsx b/src/actions/actionClipboard.tsx
index 4ca25c3f70..62a89a38f8 100644
--- a/src/actions/actionClipboard.tsx
+++ b/src/actions/actionClipboard.tsx
@@ -36,7 +36,7 @@ export const actionCut = register({
return actionDeleteSelected.perform(elements, appState);
},
contextItemLabel: "labels.cut",
- keyTest: (event) => event[KEYS.CTRL_OR_CMD] && event.code === CODES.X,
+ keyTest: (event) => event[KEYS.CTRL_OR_CMD] && event.key === KEYS.X,
});
export const actionCopyAsSvg = register({
diff --git a/src/actions/actionGroup.tsx b/src/actions/actionGroup.tsx
index 1d4b4518c5..243247efa2 100644
--- a/src/actions/actionGroup.tsx
+++ b/src/actions/actionGroup.tsx
@@ -1,4 +1,4 @@
-import { CODES, KEYS } from "../keys";
+import { KEYS } from "../keys";
import { t } from "../i18n";
import { arrayToMap, getShortcutKey } from "../utils";
import { register } from "./register";
@@ -132,7 +132,7 @@ export const actionGroup = register({
contextItemPredicate: (elements, appState) =>
enableActionGroup(elements, appState),
keyTest: (event) =>
- !event.shiftKey && event[KEYS.CTRL_OR_CMD] && event.code === CODES.G,
+ !event.shiftKey && event[KEYS.CTRL_OR_CMD] && event.key === KEYS.G,
PanelComponent: ({ elements, appState, updateData }) => (
<ToolButton
hidden={!enableActionGroup(elements, appState)}
@@ -189,7 +189,7 @@ export const actionUngroup = register({
};
},
keyTest: (event) =>
- event.shiftKey && event[KEYS.CTRL_OR_CMD] && event.code === CODES.G,
+ event.shiftKey && event[KEYS.CTRL_OR_CMD] && event.key === KEYS.G,
contextItemLabel: "labels.ungroup",
contextItemPredicate: (elements, appState) =>
getSelectedGroupIds(appState).length > 0,
diff --git a/src/actions/actionMenu.tsx b/src/actions/actionMenu.tsx
index 7a6368d46c..f2ad33f9c5 100644
--- a/src/actions/actionMenu.tsx
+++ b/src/actions/actionMenu.tsx
@@ -4,7 +4,7 @@ import { t } from "../i18n";
import { showSelectedShapeActions, getNonDeletedElements } from "../element";
import { register } from "./register";
import { allowFullScreen, exitFullScreen, isFullScreen } from "../utils";
-import { CODES, KEYS } from "../keys";
+import { KEYS } from "../keys";
import { HelpIcon } from "../components/HelpIcon";
export const actionToggleCanvasMenu = register({
@@ -67,7 +67,7 @@ export const actionFullScreen = register({
commitToHistory: false,
};
},
- keyTest: (event) => event.code === CODES.F && !event[KEYS.CTRL_OR_CMD],
+ keyTest: (event) => event.key === KEYS.F && !event[KEYS.CTRL_OR_CMD],
});
export const actionShortcuts = register({
diff --git a/src/keys.ts b/src/keys.ts
index 7704f4e2fe..92ff108415 100644
--- a/src/keys.ts
+++ b/src/keys.ts
@@ -18,11 +18,8 @@ export const CODES = {
SLASH: "Slash",
C: "KeyC",
D: "KeyD",
- G: "KeyG",
- F: "KeyF",
H: "KeyH",
V: "KeyV",
- X: "KeyX",
Z: "KeyZ",
R: "KeyR",
} as const;
@@ -47,9 +44,12 @@ export const KEYS = {
COMMA: ",",
A: "a",
+ C: "c",
D: "d",
E: "e",
+ F: "f",
G: "g",
+ H: "h",
I: "i",
L: "l",
O: "o",
diff --git a/src/tests/contextmenu.test.tsx b/src/tests/contextmenu.test.tsx
index 3e1c744791..55841b2432 100644
--- a/src/tests/contextmenu.test.tsx
+++ b/src/tests/contextmenu.test.tsx
@@ -14,7 +14,7 @@ import ExcalidrawApp from "../excalidraw-app";
import * as Renderer from "../renderer/renderScene";
import { reseed } from "../random";
import { UI, Pointer, Keyboard } from "./helpers/ui";
-import { CODES } from "../keys";
+import { KEYS } from "../keys";
import { ShortcutName } from "../actions/shortcuts";
import { copiedStyles } from "../actions/actionStyles";
import { API } from "./helpers/api";
@@ -240,7 +240,7 @@ describe("contextMenu element", () => {
});
Keyboard.withModifierKeys({ ctrl: true }, () => {
- Keyboard.codePress(CODES.G);
+ Keyboard.keyPress(KEYS.G);
});
fireEvent.contextMenu(GlobalTestState.canvas, {
@@ -537,7 +537,7 @@ describe("contextMenu element", () => {
});
Keyboard.withModifierKeys({ ctrl: true }, () => {
- Keyboard.codePress(CODES.G);
+ Keyboard.keyPress(KEYS.G);
});
fireEvent.contextMenu(GlobalTestState.canvas, {
diff --git a/src/tests/helpers/ui.ts b/src/tests/helpers/ui.ts
index a8b284d72e..1cfdb10a1d 100644
--- a/src/tests/helpers/ui.ts
+++ b/src/tests/helpers/ui.ts
@@ -3,7 +3,7 @@ import {
ExcalidrawLinearElement,
ExcalidrawTextElement,
} from "../../element/types";
-import { CODES } from "../../keys";
+import { KEYS } from "../../keys";
import { ToolName } from "../queries/toolQueries";
import { fireEvent, GlobalTestState } from "../test-utils";
import { mutateElement } from "../../element/mutateElement";
@@ -314,7 +314,7 @@ export class UI {
static group(elements: ExcalidrawElement[]) {
mouse.select(elements);
Keyboard.withModifierKeys({ ctrl: true }, () => {
- Keyboard.codePress(CODES.G);
+ Keyboard.keyPress(KEYS.G);
});
}
diff --git a/src/tests/regressionTests.test.tsx b/src/tests/regressionTests.test.tsx
index 2383460b46..5e96925b96 100644
--- a/src/tests/regressionTests.test.tsx
+++ b/src/tests/regressionTests.test.tsx
@@ -485,7 +485,7 @@ describe("regression tests", () => {
}
Keyboard.withModifierKeys({ ctrl: true }, () => {
- Keyboard.codePress(CODES.G);
+ Keyboard.keyPress(KEYS.G);
});
for (const element of h.elements) {
@@ -524,7 +524,7 @@ describe("regression tests", () => {
Keyboard.withModifierKeys({ ctrl: true }, () => {
Keyboard.keyPress(KEYS.A);
- Keyboard.codePress(CODES.G);
+ Keyboard.keyPress(KEYS.G);
});
expect(API.getSelectedElements().length).toBe(3);
@@ -561,7 +561,7 @@ describe("regression tests", () => {
mouse.click();
});
Keyboard.withModifierKeys({ ctrl: true }, () => {
- Keyboard.codePress(CODES.G);
+ Keyboard.keyPress(KEYS.G);
});
expect(h.elements.map((element) => element.id)).toEqual([
@@ -578,7 +578,7 @@ describe("regression tests", () => {
Keyboard.withModifierKeys({ ctrl: true }, () => {
Keyboard.keyPress(KEYS.A);
- Keyboard.codePress(CODES.G);
+ Keyboard.keyPress(KEYS.G);
});
mouse.doubleClickOn(rectC);
@@ -586,7 +586,7 @@ describe("regression tests", () => {
mouse.clickOn(rectA);
});
Keyboard.withModifierKeys({ ctrl: true }, () => {
- Keyboard.codePress(CODES.G);
+ Keyboard.keyPress(KEYS.G);
});
expect(rectC.groupIds.length).toBe(2);
@@ -996,7 +996,7 @@ describe("regression tests", () => {
Keyboard.withModifierKeys({ ctrl: true }, () => {
Keyboard.keyPress(KEYS.A);
- Keyboard.codePress(CODES.G);
+ Keyboard.keyPress(KEYS.G);
});
const selectedGroupIds_prev = h.state.selectedGroupIds;
@@ -1110,7 +1110,7 @@ it(
// Create group with first and third rectangle
Keyboard.withModifierKeys({ ctrl: true }, () => {
- Keyboard.codePress(CODES.G);
+ Keyboard.keyPress(KEYS.G);
});
expect(API.getSelectedElements().length).toBe(2);