Skip to main content
Home
Works with
It is unknown whether this package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
It is unknown whether this package works with Cloudflare Workers
It is unknown whether this package works with Node.js
It is unknown whether this package works with Deno
It is unknown whether this package works with Bun
It is unknown whether this package works with Browsers
JSR Score52%
Published11 months ago (1.0.6)
class Input

The Input class handles action mapping and user input event listening.

Getting started

The following code demonstrates using arrows and WASD keys for entity movement:

import { Input } from "com.hydroper.webinputaction";

Input.input.setActions({
    "moveLeft": [
        { key: "a" },
        { key: "leftArrow" },
    ],
    "moveRight": [
        { key: "d" },
        { key: "rightArrow" },
    ],
    "moveUp": [
        { key: "w" },
        { key: "upArrow" },
    ],
    "moveDown": [
        { key: "s" },
        { key: "downArrow" },
    ],
});

Input.input.addEventListener("inputPressed", () => {
    const shouldMoveRight = Input.input.isPressed("moveRight");
});

Built-in actions

The following actions are pre-defined in every action map and can be overriden:

  • escape - Used for escaping out in the user interface.
  • navigateLeft — Used for focusing the left neighbor of an user interface control.
  • navigateRight — Used for focusing the right neighbor of an user interface control.
  • navigateUp — Used for focusing the top neighbor of an user interface control.
  • navigateDown — Used for focusing the bottom neighbor of an user interface control.

Events

This class extends EventTarget and may dispatch the following events:

// Dispatched when user input starts being pressed or
// is continuously pressed.
inputPressed: Event;
// Dispatched when any user input is released.
inputReleased: Event;
// Dispatched when the actions map is updated.
actionsUpdated: Event;

Properties

private
mMap: Record<string, InputActionAtom[]>

Methods

Returns the current action map in read-only mode.

Determines whether an action is pressed.

Determines whether an action has been just pressed right now.

setActions(map: Record<string, InputActionAtom[]>): void

Updates the action map.

Static Properties

readonly
input: Input

The singleton instance of the Input class.

private
readonly
mPressedStatePoolKeys: Map<InputActionKeyName, PressedState>

Static Methods

private
builtin(): Record<string, InputActionAtom[]>

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@hydroper/inputaction

Import symbol

import Input from "@hydroper/inputaction";
or

Import directly with a jsr specifier

import Input from "jsr:@hydroper/inputaction";

Add Package

pnpm i jsr:@hydroper/inputaction
or (using pnpm 10.8 or older)
pnpm dlx jsr add @hydroper/inputaction

Import symbol

import Input from "@hydroper/inputaction";

Add Package

yarn add jsr:@hydroper/inputaction
or (using Yarn 4.8 or older)
yarn dlx jsr add @hydroper/inputaction

Import symbol

import Input from "@hydroper/inputaction";

Add Package

vlt install jsr:@hydroper/inputaction

Import symbol

import Input from "@hydroper/inputaction";

Add Package

npx jsr add @hydroper/inputaction

Import symbol

import Input from "@hydroper/inputaction";

Add Package

bunx jsr add @hydroper/inputaction

Import symbol

import Input from "@hydroper/inputaction";