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

hotkeys-inputs-js

v1.1.21

Published

A javascript library for handling both keyboard and gamepad inputs in web applications.

Downloads

50

Readme

hotkeys-inputs-js

hotkeys-inputs-js is a javascript library for handling both keyboard and gamepad inputs in web applications. This is the ideal solution for your project if you need to work with both a keyboard and gamepad, and frequently need to activate or deactivate input events to manage actions.

GitHub package version NPM Jest coverage

Features

  • Subscribe to gamepad events such as button presses and joystick movements
  • Subscribe to keyboard events such as key presses
  • Register actions with a group of different keys or buttons
  • Unify input handling with a single, user-friendly interface

Installation

npm install hotkeys-input-js

or

yarn add hotkeys-input-js

Simple usage

// Action definition
const defaultMapping = {
  directionY: [
    { type: 'keyboard', key: 'w', options: { event: 'repeat', value: 1 } },
    { type: 'keyboard', key: 's', options: { event: 'repeat', value: -1 } },
    { type: 'gamepad', key: DefaultGamepad.LeftJoystickAxeY, options: { event: 'repeat', value: -2 } },
  ],
  directionX: [
    { type: 'keyboard', key: 'd', options: { event: 'repeat', value: 1 } },
    { type: 'keyboard', key: 'a', options: { event: 'repeat', value: -1 } },
    { type: 'gamepad', key: DefaultGamepad.LeftJoystickAxeX, options: { event: 'repeat', value: 2 } },
  ],
};

// Register previously defined actions
inputActions.defineInputActions(defaultMapping);

// Register events from the group of action, defines callback for when an event is triggered
inputActions.onInputActions(`moveActions`, {
  directionX: (v) => moveX(v),
  directionY: (v) => moveY(v),
});

API Reference

defineInputActions

To specify the different sets of actions that your application will support, employ the inputActions.defineInputActions function. Note that this function does not attach any event listeners; rather, it serves as a way to enumerate all the actions that your app can handle. When utilizing the override parameter, any previously defined actions will be replaced, while not providing this parameter will result in an error being thrown.

Mapping an action to handlers

interface MapExample {
  [actionNames]: {
    type: 'keyboard' | 'gamepad';
    key: number | string;
    options: {
      event: 'changed' | 'pressed' | 'released' | 'repeat';
      value: number;
    };
  };
}

actionNames: The keys of the object represent the action names that the mappings are associated with.

handlers: An array of objects that specify the inputs that trigger the associated action.

type: Indicates the input device type, either keyboard or gamepad.

key: Specifies the input key, which is either an integer for gamepad button ID or a string using the js-keyboard library. event: The type of input event, which can be repeat, pressed, released, or changed.

value: The value parameter that the callback function is called with. If the key is pressed, the value will be the value specified. If the key is not pressed, the value will be 0. For analog inputs such as joysticks, the value will reflect the degree of input (e.g., if a joystick is at 50%, the value will be 0.5).

Example

const mapping1 = {
  directionY: [{ type: 'keyboard', key: 'w', options: { event: 'repeat', value: 1 } }],
  directionX: [{ type: 'keyboard', key: 'd', options: { event: 'repeat', value: 1 } }],
};

const mapping2 = {
  jump: [{ type: 'keyboard', key: 'space', options: { event: 'pressed', value: 1 } }],
  punch: [{ type: 'keyboard', key: '1', options: { event: 'pressed', value: 1 } }],
};

// works
inputActions.defineInputActions(mapping1);
inputActions.defineInputActions(mapping2);

// throw an error, it has already been defined
inputActions.defineInputActions(mapping1);

// works
inputActions.defineInputActions(mapping1, { override: true });

onInputActions

The inputActions.onInputActions function allows you to register a collection of actions to functions, which will be invoked with the value provided in the mapping when a given action is triggered. The function takes the following parameters:

inputActions.onInputActions(id: string, handlers: Record<string, function>, unsubscribedCallback: function )

id: The ID of the group of actions to subscribe. The group ID must be unique and no duplicates are allowed. You can use this ID to later unsubscribe the group of actions related to it.

handlers: The collection of actions and callbacks to invoke when the key corresponding to the mapping is triggered by the user. If the action is not defined, an error will be thrown.

unsubscribedCallback: (Optional) The unsubscribedCallback is invoked when the group of actions is unregistered.

const mapping1 = {
  directionY: [{ type: 'keyboard', key: 'w', options: { event: 'repeat', value: 1 } }],
  directionX: [{ type: 'keyboard', key: 'd', options: { event: 'repeat', value: 1 } }],
};

inputActions.defineInputActions(mapping1);

inputActions.onInputActions(
  `moveActions`,
  {
    directionX: (v) => moveX(v),
    directionY: (v) => moveY(v),
  },
  myCallbackWhenUnregisters,
);

offInputActions

The offInputActions function is used to unregister a previously registered group of input actions. When called, this function will unregister the group of actions that corresponds to the given ID. The function takes a single parameter:

id: The ID of the group of actions to unsubscribe.

inputActions.offInputActions(`moveActions`);

Enable or disable handlers

You can enable or disable the support for an input handler. If the handler is disabled, it will not fire the action if the input is triggered.

Note that only the keyboard is enabled by default

//enable
inputActions.keyboardEnabled = true;
inputActions.gamepadEnabled = true;

//disable
inputActions.keyboardEnabled = false;
inputActions.gamepadEnabled = false;

Testing

I have been using node 14.18.0 for the development

  • Clone the repository
  • Run npm install and then npm build
  • use the index.html
  • Happy coding!

Contributing

If you would like to contribute to the development of this library, please reach out to us. We welcome contributions of all kinds, from bug reports and fixes to new features and improvements.