Skip to content

Commit

Permalink
References for keys and meta key handling (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcusLongmuir authored Oct 17, 2023
1 parent f11a70e commit 27643b0
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions packages/3d-web-client-core/src/input/KeyInputManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { EventHandlerCollection } from "./EventHandlerCollection";

enum Key {
W = "w",
A = "a",
S = "s",
D = "d",
SHIFT = "shift",
SPACE = " ",
}

export class KeyInputManager {
private keys = new Map<string, boolean>();
private eventHandlerCollection = new EventHandlerCollection();
Expand All @@ -16,6 +25,14 @@ export class KeyInputManager {

private onKeyDown(event: KeyboardEvent): void {
if (this.shouldCaptureKeyPress()) {
if (event.key.length === 2 && event.key[0] === "F") {
// Ignore all Function keys
return;
}
if (event.metaKey) {
// Ignore all meta keys (e.g. Alt, Cmd)
return;
}
this.keys.set(event.key.toLowerCase(), true);
event.preventDefault();
}
Expand All @@ -30,31 +47,31 @@ export class KeyInputManager {
}

public isMovementKeyPressed(): boolean {
return ["w", "a", "s", "d"].some((key) => this.isKeyPressed(key));
return [Key.W, Key.A, Key.S, Key.D].some((key) => this.isKeyPressed(key));
}

get forward(): boolean {
return this.isKeyPressed("w");
return this.isKeyPressed(Key.W);
}

get backward(): boolean {
return this.isKeyPressed("s");
return this.isKeyPressed(Key.S);
}

get left(): boolean {
return this.isKeyPressed("a");
return this.isKeyPressed(Key.A);
}

get right(): boolean {
return this.isKeyPressed("d");
return this.isKeyPressed(Key.D);
}

get run(): boolean {
return this.isKeyPressed("shift");
return this.isKeyPressed(Key.SHIFT);
}

get jump(): boolean {
return this.isKeyPressed(" ");
return this.isKeyPressed(Key.SPACE);
}

get anyDirection(): boolean {
Expand All @@ -63,8 +80,8 @@ export class KeyInputManager {

get conflictingDirection(): boolean {
return (
(this.isKeyPressed("w") && this.isKeyPressed("s")) ||
(this.isKeyPressed("a") && this.isKeyPressed("d"))
(this.isKeyPressed(Key.W) && this.isKeyPressed(Key.S)) ||
(this.isKeyPressed(Key.A) && this.isKeyPressed(Key.D))
);
}

Expand Down

0 comments on commit 27643b0

Please sign in to comment.