Skip to content

Commit

Permalink
chore: start getKeysDown
Browse files Browse the repository at this point in the history
  • Loading branch information
lajbel committed Jan 24, 2025
1 parent 7f9d267 commit 6b3fb39
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ There is the structure of a JSDoc comment:
- `@returns` for the return value description.
- `@since` for the version when the member was added.
- `@group` for the group of the member.
- `@experimental` for indicate smt is in experimental phase

Example:

Expand Down
32 changes: 32 additions & 0 deletions src/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,32 @@ export const initApp = (opt: {
state.events.trigger("buttonRelease", btn);
}

// #region Pressed keys getters
function getKeysPressed(): Key[] {
return Array.from(state.keyState.pressed);
}

function getKeysDown(): Key[] {
return Array.from(state.keyState.down);
}

function getKeysReleased(): Key[] {
return Array.from(state.keyState.released);
}

function getButtonsPressed(): string[] {
return Array.from(state.buttonState.pressed);
}

function getButtonsDown(): string[] {
return Array.from(state.buttonState.down);
}

function getButtonsReleased(): string[] {
return Array.from(state.buttonState.released);
}
// #endregion

function onResize(action: () => void): KEventController {
return state.events.on("resize", action);
}
Expand Down Expand Up @@ -1240,6 +1266,12 @@ export const initApp = (opt: {
getButton,
pressButton,
releaseButton,
getKeysPressed,
getKeysDown,
getKeysReleased,
getButtonsPressed,
getButtonsDown,
getButtonsReleased,
charInputted,
onResize,
onKeyDown,
Expand Down
24 changes: 24 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3584,6 +3584,30 @@ export interface KAPLAYCtx<
* @group Input
*/
releaseButton(btn: TButton): void;
/**
* Get all keys that was pressed current frame.
*
* @example
* ```js
* onMousePress(() => {
* const keys = getKeysPressed();
*
* if(keys.includes("k")) {
* debug.log("You was pressing K!")
* }
* });
* ```
*
* @returns A list of pressed keys.
* @since v3001.0.10
* @group Input
* @experimental This feature is in experimental phase, it will be fully released in v3001.1.0
*/
getKeysPressed(): Key[];
/**
* Get all keys that are held down in current frame.
*/
getKeysDown(): Key[];
/**
* Get stick axis values from a gamepad.
*
Expand Down

0 comments on commit 6b3fb39

Please sign in to comment.