Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion packages/input/src/input-handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InputEnum } from "./input.enum";
import { InputEnum, MouseEnum } from "./input.enum";

export class InputHandler {
public inputs: Record<string, boolean> = {};
Expand All @@ -12,6 +12,20 @@ export class InputHandler {
this.inputs[e.code] = false;
});

window.addEventListener("mousedown", (e: MouseEvent) => {
const mouseButton = MouseEnum[e.button];
if (!mouseButton) return;

this.inputs[mouseButton] = true;
});

window.addEventListener("mouseup", (e: MouseEvent) => {
const mouseButton = MouseEnum[e.button];
if (!mouseButton) return;

this.inputs[mouseButton] = false;
});

for (const key in InputEnum) {
this.inputs[key] = false;
}
Expand Down
13 changes: 13 additions & 0 deletions packages/input/src/input.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,17 @@ export enum InputEnum {
BrowserBack = "BrowserBack",
LaunchApp1 = "LaunchApp1",
LaunchMail = "LaunchMail",
MouseLeft = "MouseLeft",
MouseMiddle = "MouseMiddle",
MouseRight = "MouseRight",
Back = "BackButton",
Forward = "Forward",
}

export enum MouseEnum {
MouseLeft,
MouseMiddle,
MouseRight,
Back,
Forward,
}
34 changes: 30 additions & 4 deletions packages/input/test/input.library.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

import { InputEnum, InputLibrary } from "../src";

type MockInputEvent = {
code?: string;
button?: number;
};

const makeWindowMock = () => {
const listeners: Record<string, ((e: KeyboardEvent) => void)[]> = {};
const listeners: Record<string, ((e: MockInputEvent) => void)[]> = {};
return {
addEventListener: vi.fn((event: string, handler: (e: KeyboardEvent) => void) => {
addEventListener: vi.fn((event: string, handler: (e: MockInputEvent) => void) => {
if (!listeners[event]) listeners[event] = [];
listeners[event].push(handler);
}),
dispatch: (event: string, e: Partial<KeyboardEvent>) => {
listeners[event]?.forEach((h) => h(e as KeyboardEvent));
dispatch: (event: string, e: Partial<MockInputEvent>) => {
listeners[event]?.forEach((h) => h(e as MockInputEvent));
},
};
};
Expand Down Expand Up @@ -96,5 +101,26 @@ describe("InputLibrary", () => {
expect(pressed).not.toContain(InputEnum.KeyA);
expect(pressed).toContain(InputEnum.Space);
});

it("should return true for left mouse after mousedown", () => {
windowMock.dispatch("mousedown", { button: 0 });
expect(library.isKeyPressed(InputEnum.MouseLeft)).toBe(true);
});

it("should return false for left mouse after mousedown then mouseup", () => {
windowMock.dispatch("mousedown", { button: 0 });
windowMock.dispatch("mouseup", { button: 0 });
expect(library.isKeyPressed(InputEnum.MouseLeft)).toBe(false);
});

it("should handle unknown mouse input", () => {
windowMock.dispatch("mousedown", { button: -2 });
windowMock.dispatch("mousedown", { button: 999 });
windowMock.dispatch("mouseup", { button: 999 });

const pressed = library.getPressedKeys();

expect(pressed).toHaveLength(0);
});
});
});
Loading