Skip to content

Commit

Permalink
feat: add keyboardCode on input bindings (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
lajbel authored and web-flow committed Aug 30, 2024
1 parent 9d9d558 commit 070dd4e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ features of v4000, while v4000 will have the most features and breaking changes.
buttons: {
jump: {
keyboard: ["space", "up"],
keyboardCode: "Space", // you can also use key codes
gamepad: ["south"],
},
},
Expand Down
18 changes: 18 additions & 0 deletions src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const initAppState = (opt: {
buttonsByKey: new Map<Key, string[]>(),
buttonsByMouse: new Map<MouseButton, string[]>(),
buttonsByGamepad: new Map<KGamepadButton, string[]>(),
buttonsByKeyCode: new Map<string, string[]>(),
loopID: null as null | number,
stopped: false,
dt: 0,
Expand Down Expand Up @@ -962,6 +963,8 @@ export const initApp = (opt: {
state.events.onOnce("input", () => {
const k: Key = KEY_ALIAS[e.key as keyof typeof KEY_ALIAS] as Key
|| e.key.toLowerCase();
const code = e.code;

if (k === undefined) throw new Error(`Unknown key: ${e.key}`);
if (k.length === 1) {
state.events.trigger("charInput", k);
Expand All @@ -985,6 +988,13 @@ export const initApp = (opt: {
});
}

if (state.buttonsByKeyCode.has(code)) {
state.buttonsByKeyCode.get(code)?.forEach((btn) => {
state.buttonState.press(btn);
state.events.trigger("buttonPress", btn);
});
}

state.keyState.press(k);
state.events.trigger("keyPressRepeat", k);
state.events.trigger("keyPress", k);
Expand All @@ -996,6 +1006,7 @@ export const initApp = (opt: {
state.events.onOnce("input", () => {
const k: Key = KEY_ALIAS[e.key as keyof typeof KEY_ALIAS] as Key
|| e.key.toLowerCase();
const code = e.code;

if (state.buttonsByKey.has(k)) {
state.buttonsByKey.get(k)?.forEach((btn) => {
Expand All @@ -1004,6 +1015,13 @@ export const initApp = (opt: {
});
}

if (state.buttonsByKeyCode.has(code)) {
state.buttonsByKeyCode.get(code)?.forEach((btn) => {
state.buttonState.release(btn);
state.events.trigger("buttonRelease", btn);
});
}

state.keyState.release(k);
state.events.trigger("keyRelease", k);
});
Expand Down
9 changes: 9 additions & 0 deletions src/app/inputBindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { appState } from "./app";
*/
export type ButtonBinding = {
keyboard?: Key | Key[];
keyboardCode?: string | string[];
gamepad?: KGamepadButton | KGamepadButton[];
mouse?: MouseButton | MouseButton[];
};
Expand Down Expand Up @@ -38,6 +39,8 @@ export const parseButtonBindings = () => {

for (const b in btns) {
const keyboardBtns = btns[b].keyboard && [btns[b].keyboard].flat();
const keyboardCodes = btns[b].keyboardCode
&& [btns[b].keyboardCode].flat();
const gamepadBtns = btns[b].gamepad && [btns[b].gamepad].flat();
const mouseBtns = btns[b].mouse && [btns[b].mouse].flat();

Expand All @@ -47,6 +50,12 @@ export const parseButtonBindings = () => {
});
}

if (keyboardCodes) {
keyboardCodes.forEach((k) => {
mapAddOrPush(appState.buttonsByKeyCode, k, b);
});
}

if (gamepadBtns) {
gamepadBtns.forEach((g) => {
mapAddOrPush(appState.buttonsByGamepad, g, b);
Expand Down

0 comments on commit 070dd4e

Please sign in to comment.