Skip to content

Commit 025bc26

Browse files
committed
feat: add support of default keys for editor camera using keyboard layout
#723
1 parent 2d02252 commit 025bc26

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

editor/src/editor/nodes/camera.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export class EditorCamera extends FreeCamera {
9191
this.keysUpward = keys.keysUpward;
9292
this.keysDownward = keys.keysDownward;
9393
} else {
94-
this._setDefaultKeys();
94+
this._setDefaultKeysFromLayout();
9595
}
9696

9797
// Load pan sensitivity multiplier if available
@@ -100,7 +100,7 @@ export class EditorCamera extends FreeCamera {
100100
}
101101
} catch (e) {
102102
// If no preferences found or error occurred, use defaults
103-
this._setDefaultKeys();
103+
this._setDefaultKeysFromLayout();
104104
}
105105
}
106106

@@ -138,14 +138,50 @@ export class EditorCamera extends FreeCamera {
138138
this._panInput.panSensitivityMultiplier = value;
139139
}
140140

141-
private _setDefaultKeys() {
141+
private _setDefaultKeys(): void {
142142
this.keysUp = DEFAULT_KEYS.keysUp;
143143
this.keysDown = DEFAULT_KEYS.keysDown;
144144
this.keysLeft = DEFAULT_KEYS.keysLeft;
145145
this.keysRight = DEFAULT_KEYS.keysRight;
146146
this.keysUpward = DEFAULT_KEYS.keysUpward;
147147
this.keysDownward = DEFAULT_KEYS.keysDownward;
148148
}
149+
150+
private _setDefaultKeysFromLayout(): void {
151+
this._setDefaultKeys();
152+
153+
navigator.keyboard?.getLayoutMap().then((layout) => {
154+
const keyUp = layout.get("KeyW")?.toUpperCase().charCodeAt(0);
155+
if (keyUp) {
156+
this.keysUp = [keyUp];
157+
}
158+
159+
const keyDown = layout.get("KeyS")?.toUpperCase().charCodeAt(0);
160+
if (keyDown) {
161+
this.keysDown = [keyDown];
162+
}
163+
164+
const keyLeft = layout.get("KeyA")?.toUpperCase().charCodeAt(0);
165+
if (keyLeft) {
166+
this.keysLeft = [keyLeft];
167+
}
168+
169+
const keyRight = layout.get("KeyD")?.toUpperCase().charCodeAt(0);
170+
if (keyRight) {
171+
this.keysRight = [keyRight];
172+
}
173+
174+
const keyUpward = layout.get("KeyE")?.toUpperCase().charCodeAt(0);
175+
if (keyUpward) {
176+
this.keysUpward = [keyUpward];
177+
}
178+
179+
const keyDownward = layout.get("KeyQ")?.toUpperCase().charCodeAt(0);
180+
if (keyDownward) {
181+
this.keysDownward = [keyDownward];
182+
}
183+
});
184+
}
149185
}
150186

151187
Node.AddNodeConstructor("EditorCamera", (name, scene) => {

editor/src/tools/augmentations.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* eslint-disable @typescript-eslint/naming-convention */
2+
3+
interface Keyboard {
4+
getLayoutMap(): Promise<KeyboardLayoutMap>;
5+
}
6+
7+
interface KeyboardLayoutMap {
8+
get(code: string): string | undefined;
9+
entries(): IterableIterator<[string, string]>;
10+
}
11+
12+
declare interface Navigator {
13+
keyboard?: Keyboard;
14+
}

0 commit comments

Comments
 (0)