Skip to content
This repository has been archived by the owner on Mar 3, 2024. It is now read-only.
/ buttonmancer Public archive

Action mapping for gamepads and keyboards based on normalized button names.

License

Notifications You must be signed in to change notification settings

DVDAGames/buttonmancer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

buttonmancer 0.2.2

Easily facilitate mapping gamepad buttons and keyboard keys to specific actions in your game.

Buttonmancer supports multiple alternate keys for each action, remapping controls, and returning to default controls;

First you need to install buttonmancer:

npm install @dvdagames/buttonmancer

Just pass in an Action Map to the Buttonmancer() class like the example below to get started.

import Buttonmancer from 'buttonmancer';

const actionMap = {
  gamepad: {
    MOVE_LEFT: ['Left'],
    MOVE_RIGHT: ['Right'],
    JUMP: ['X']
  },
  keyboard: {
    MOVE_LEFT: ['ArrowLeft', 'KeyA'],
    MOVE_RIGHT: ['ArrowRight', 'KeyD'],
    JUMP: ['SpaceBar']
  }
};

const controls = new Buttonmancer(actionMap.gamepad);

You can remap all of the controls in your button mapping by using the remap() method and passing in a new Action Map.

const newMap = {
  MOVE_LEFT: ['Up'],
  MOVE_RIGHT: ['Down'],
  JUMP: ['X', 'Circle', 'Sqaure', 'R1']
};

controls.remap(newMap);

You can remap a single control by using the alterControl() method and passing a single Action.

const newLeft = {
  MOVE_LEFT: ['Left', 'L1']
};

controls.alterControl(newLeft);

Buttonmancer also comes with some utilities, including a simple way to get an accurate and consistent keyboard event name.

import ButtonmancerUtils from 'buttonmancer';

document.addEventListener('keydown', (e) => {
  const keyPressed = ButtonmancerUtils.getKey(e);
});

Currently this function uses the e.keyCode or e.which value to return a normalized e.key value because browser implementations are inconsistent. In the future, it will use the appropriate e.key or e.keyIdentifier value to normalize the event name.

About

Action mapping for gamepads and keyboards based on normalized button names.

Topics

Resources

License

Stars

Watchers

Forks

Packages