npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pixi-input-map

v1.0.0

Published

Pixi Input Map is used to manage input actions which are named events tied to a key or mouse button.

Downloads

7

Readme

Pixi Input Map

Pixi Input Map is used to associate named actions with keyboard keys or mouse buttons. These actions, keys, and buttons can be checked if they're being used and they can also be simulated.

Note: Despite the name, this library doesn't require Pixi.

Install

npm install pixi-input-map

Usage

Pixi Input Map exports a singleton because there should only ever be once instance of it since it attaches event listeners to the window for the keyboard and mouse.

Importing

There's currently one export, a class named PixiInputMap. You can import it like so:

import { PixiInputMap } from "pixi-input-map";

Creating the Instance

You should only ever have one instance of PixiInputMap created because otherwise, you'll set up multiple keyboard and mouse listeners that might conflict with each other. So before using it, you should create the instance in a place that can be imported into wherever you need it in your app.

export const inputMap = new PixiInputMap();

Starting the Listeners

Next, to start listening for events, you have to call the start method:

pixiInputMap.start();

This is not automatic because you might not want to start listening to events right away. You can call this when your game starts or just whenever you're ready to start handling input.

Note: There's also a stop method, which removes the event listeners and cleans up the internal cache.

Using Actions

At this point, you can create an action with a name and the key/mouse button that triggers it.

pixiInputMap.addAction("jump", "Space");

Now, in your game loop, you can check for the action being used.

const gameLoop = (delta: number) => {
    const isJumping = pixiInputMap.isActionPressed("jump");
};

Manually Triggering and Releasing Actions

You can also manually trigger actions without user input with the actionPress and actionRelease methods.

Note: You'll have to call actionRelease after actionPress or else it will behave as if the user is holding down the action.

pixiInputMap.addAction("jump", "Space");

pixiInputMap.actionPress("jump");
pixiInputMap.actionRelease("jump");

Checking If a Key or Mouse Button Is Pressed

Along with checking if actions are being used, you can also check if a specific key or mouse button is being pressed or not.

const isSpacePressed = pixiInputMap.isKeyPressed("Space");

const isLeftMouseButtonPressed = pixiInputMap.isMouseButtonPressed(0);

Checking If Anything Is Pressed**

You can also check to see if any key or mouse button is pressed. This could be useful in situations where you want to take an action if the user interacts with the game at all.

const isAnythingPressed = pixiInputMap.isAnythingPressed();

API

start

Starts listening for keyboard and mouse events.

Example

pixiInputMap.start();

stop

Stops listening for keyboard and mouse events and resets the internal cache.

Example

pixiInputMap.stop();

addAction

Adds an action with a keyboard key or mouse button that triggers the event.

| Param | Description | | ----- | ---------------------------------------------------------------- | | name | The name of the action, used when checking if it is used or not. | | key | The keyboard key or mouse button that triggers the event. |

Note: The param is named key but it can be a keyboard key or mouse button.

They key should be taken from the MDN KeyboardEvent.key docs.

Mouse buttons will vary based on the mouse but general information can be found on the MDN MouseEvent.button docs.

Example

pixiInputMap.addAction("jump", "Space");

isActionPressed

Returns whether the action with the specified name is being used or not. This should be checked in your game loop.

| Param | Description | | ----- | ----------------------------------------------------- | | name | The name of the action to check if being used or not. |

Example

pixiInputMap.addAction("jump", "Space");

const gameLoop = () => {
    const isJumping = pixiInputMap.isActionPressed("jump");
};

actionPress

Manually triggers an action. This is equivalent to the user pressing and holding the key or mouse button.

Note: You'll have to call actionRelease to simulate the user releasing the key or mouse button.

| Param | Description | | ----- | -------------------------------- | | name | The name of the action to press. |

Example

pixiInputMap.addAction("jump", "Space");
pixiInputMap.pressAction("jump");

actionRelease

Manually releases an action. This is equivalent to the user releasing the key or mouse button.

| Param | Description | | ----- | ---------------------------------- | | name | The name of the action to release. |

Example

pixiInputMap.addAction("jump", "Space");

pixiInputMap.pressAction("jump");
pixiInputMap.releaseAction("jump");

isKeyPressed

Returns whether the specified key is being used or not. This should be checked in your game loop.

| Param | Description | | ----- | ------------------------------- | | key | The key to check if being used. |

Example

const isSpacePressed = pixiInputMap.isKeyPressed("Space");

isMouseButtonPressed

Returns whether the specified mouse button is being used or not. This should be checked in your game loop.

| Param | Description | | ----------- | ---------------------------------------- | | mouseButton | The mouse button to check if being used. |

Example

const isLeftMouseButtonPressed = pixiInputMap.isMouseButtonPressed(0);

isAnythingPressed

Returns whether any key or mouse button is being used or not. This should be checked in your game loop.

Example

const isAnythingPressed = pixiInputMap.isAnythingPressed();

License

MIT