nes-cntlr

Javascript NES controller emulator for keyboards and touch devices

Usage no npm install needed!

<script type="module">
  import nesCntlr from 'https://cdn.skypack.dev/nes-cntlr';
</script>

README

nes-cntlr

Javascript NES controller emulator for keyboards and touch devices

Allow you to use your keyboard and/or a virtual NES Controller (for touch devices) to control whatever you create, like you would with a NES emulator.

alt text

npm i nes-cntlr

Demo

Usage

import NESCntlr from 'nes-cntlr';
let player1 = new NESCntlr();
player1.init();

Events

All events return a 'pressed' parameter [bool: true|false]. On release, a 'duration' parameter [number: ms] will also be provided.
You can listen to those events for your game/project.

Event Params Description
player1:up pressed, duration D-pad
player1:up-right pressed, duration D-pad
player1:right pressed, duration D-pad
player1:down-right pressed, duration D-pad
player1:down pressed, duration D-pad
player1:down-left pressed, duration D-pad
player1:left pressed, duration D-pad
player1:up-left pressed, duration D-pad
player1:select pressed, duration Select button
player1:start pressed, duration Start button
player1:b pressed, duration B button
player1:a pressed, duration A button

The 'player1' at the beginning of every event match the prefix setting. That way, you can bind multiple nes-cntlr and differentiate which one sent you an event.

Settings

Option Type Default Description
colors array ['#2F3335', '#383d41', '#AC3C66', '#D64A80', '#B4B4B4'] Black, Light black, Red, Light red, Grey
keys object {
start: 'Enter',
select: 'Space',
left: 'ArrowLeft',
up: 'ArrowUp', right: 'ArrowRight',
down: 'ArrowDown',
b: 'KeyZ',
a: 'KeyX'
}
keys mapping.
location string 'body' Allows you to select where the virtual controller should be injected. ex: .myDiv, #myDiv, etc.
prefix string 'player1' Class prefix
virtual string 'auto' 'auto', 'never' or 'always' show virtual controller.
zIndex number 100 z-index of the virtual controller.

Browser support

nes-cntlr works on all modern browsers such as Chrome, Firefox, Edge and Safari.

Dependencies

License

Copyright (c) 2018 Simon Arnold

Licensed under the MIT license.