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

user-input-mapping

v1.4.2

Published

Map keyboard, mouse, gamepad, and touch inputs from user-input.

Downloads

11

Readme

user-input-mapping

Create mappings for user inputs.

Usage

NPM

Quick example.

var Mapping   = require("user-input-mapping")
var userInput = require("user-input")
     
// Create an input listener.
var input   = userInput().withKeyboard()

// Create a mapping to translate the inputs.
var mapping = new Mapping(input, {
    keyboard: {
        left:  "A",
        right: function (k) {
            return k("A") === 1
        }
    }
})

mapping.value('right');    // Equals 0 or 1 depending on the state of key A.
mapping.value('right', 1); // Set the value of right to 1.
mapping.clear();           // Clear the values.
mapping.value.clear()      // Clear the values, useful if you export .value()

See below for test cases.

it("should do mapping stuff", function () {
    var input   = userInput().withKeyboard()
    var mapping = new Mapping(input, {
        keyboard: {
            left:  "A",
            right: function (k) {
                return k("A") === 1
            }
        }
    })
    expect(mapping.value('left')).toEqual(0)
    expect(mapping.value('right')).toEqual(0)
    input.keyboard('A', 1)
    expect(mapping.value('left')).toEqual(0)
    expect(mapping.value('right')).toEqual(0)

    mapping.update()
    expect(mapping.value('left')).toEqual(1)
    expect(mapping.value('right')).toEqual(1)

    input.keyboard('A', 0)
    expect(mapping.value('left')).toEqual(1)
    expect(mapping.value('right')).toEqual(1)

    mapping.update()
    expect(mapping.value('left')).toEqual(0)
    expect(mapping.value('right')).toEqual(0)

    mapping.value('right', 'hello')
    expect(mapping.value('right')).toEqual('hello')
    mapping.update()
    expect(mapping.value('right')).toEqual(0) // Because of mapping, will resolve back to 0 during update.

    mapping.value('greeting', 'hello')        // Not in the map.
    expect(mapping.value('greeting')).toEqual('hello')
    mapping.update()
    expect(mapping.value('greeting')).toEqual('hello')

    input.keyboard('A', 1)
    mapping.update()
    expect(mapping.value('left')).toEqual(1)
    expect(mapping.value('right')).toEqual(1)
})

it("should do mapping stuff, requireUpdate = false", function () {
    var input   = userInput().withKeyboard()
    var mapping = new Mapping(input, {
        keyboard: {
            left:  "A",
            right: function (k) {
                return k("A") === 1
            }
        }
    }, false)
    expect(mapping.value('left')).toEqual(0)
    expect(mapping.value('right')).toEqual(0)
    input.keyboard('A', 1)
    expect(mapping.value('left')).toEqual(1)
    expect(mapping.value('right')).toEqual(1)

    input.keyboard('A', 0)
    expect(mapping.value('left')).toEqual(0)
    expect(mapping.value('right')).toEqual(0)

    mapping.value('right', 'hello')
    expect(mapping.value('right')).toEqual(0) // Because of mapping, will resolve back to 0 during update.

    mapping.value('greeting', 'hello')
    expect(mapping.value('greeting')).toEqual('hello')

    input.keyboard('A', 1)
    expect(mapping.value('left')).toEqual(1)
    expect(mapping.value('right')).toEqual(1)
})

it("function mapping test", function() {
    var input   = userInput().withKeyboard()
    var mapping = new Mapping(input, {
        keyboard: {
            left:  "A",
            right: function (k) {
                return k("A") / 2;
            }
        }
    }, false)
    expect(mapping.value('left')).toEqual(0)
    expect(mapping.value('right')).toEqual(0)
    input.keyboard('A', 1)
    expect(mapping.value('left')).toEqual(1)
    expect(mapping.value('right')).toEqual(.5)
    input.keyboard('A', 11)
    expect(mapping.value('left')).toEqual(11)
    expect(mapping.value('right')).toEqual(5.5)
})

it("handles arrays in the mapping", function() {
    var input   = userInput().withKeyboard()
    var mapping = new Mapping(input, {
        keyboard: {
            left:  ["A", "B"],
            right: [
                function (k) {
                    return k("A") / 2;
                },
                function (k) {
                    return k("B") * 2;
                },
                "B"
            ]
        }
    }, false)
    expect(mapping.value('left')).toEqual(0)
    expect(mapping.value('right')).toEqual(0)
    input.keyboard('A', 1)
    input.keyboard('B', 1)
    expect(mapping.value('left')).toEqual(2)
    expect(mapping.value('right')).toEqual(3.5)
    input.keyboard('A', 11)
    input.keyboard('B', 5)
    expect(mapping.value('left')).toEqual(16)
    expect(mapping.value('right')).toEqual(20.5)
});

it("can have it's values cleared", function () {
    var input = userInput().withKeyboard().withMouse();
    var mapping = new Mapping(input, {
        keyboard: {
            left: "A"
        },
        mouse: {
            button0: "mouse0"
        }
    }, false)
    expect(mapping.value('left')).toEqual(0)
    expect(mapping.value('button0')).toEqual(0)
    expect(mapping.value('test')).toEqual(0)
    input.keyboard('A', 1)
    input.mouse('mouse0', 1)
    mapping.value('test', 1);
    expect(mapping.value('left')).toEqual(1)
    expect(mapping.value('button0')).toEqual(1)
    expect(mapping.value('test')).toEqual(1)

    // .clear()
    mapping.clear();
    expect(mapping.value('left')).toEqual(0)
    expect(mapping.value('button0')).toEqual(0)
    expect(mapping.value('test')).toEqual(0)

    // .value.clear() alias
    input.keyboard('A', 1)
    input.mouse('mouse0', 1)
    mapping.value('test', 1);
    expect(mapping.value('left')).toEqual(1)
    expect(mapping.value('button0')).toEqual(1)
    expect(mapping.value('test')).toEqual(1)

    mapping.value.clear();
    expect(mapping.value('left')).toEqual(0)
    expect(mapping.value('button0')).toEqual(0)
    expect(mapping.value('test')).toEqual(0)
})

Example (source)

var util      = require("util")
var userInput = require("user-input")
var Mapping   = require("user-input-mapping")
var Player    = require("./player.js")

module.exports = PlayerHuman

/**
 * @class
 * @extends Player
 */
function PlayerHuman(options) {
    var that      = this
    options       = options || {}
    options.input = options.input || userInput().withKeyboard().withMouse().withGamepad(0)
    Player.call(this, options)

    if (options.ship) {
        this.ship = options.ship
        this.ships.push(options.ship)
    }

    var mapping = {
        keyboard: {
            left:    "A",
            right:   "D",
            forward: "W",
            reverse: "S",
            fire:    "<space>"
        },
        mouse:    {
            rotation: function (mouse) {
                return Math.atan2(mouse("y") - that.ship.y, mouse("x") - that.ship.x)
            }
        },
        gamepad:  {
            left:     function (gamepad) {
                return gamepad.axes[0] < -.25 ? -gamepad.axes[0] : 0
            },
            right:    function (gamepad) {
                return gamepad.axes[0] > .25 ? gamepad.axes[0] : 0
            },
            forward:       function (gamepad) {
                return gamepad.axes[1] < -.25 ? -gamepad.axes[1] : 0
            },
            reverse:     function (gamepad) {
                return gamepad.axes[1] > .25 ? gamepad.axes[1] : 0
            },
            rotation: function (gamepad) {
                return Math.atan2(gamepad.axes[3], gamepad.axes[2])
            }
        }
    };

    this.input   = options.input
    this.mapping = new Mapping(this.input, mapping)
}
util.inherits(PlayerHuman, Player)

PlayerHuman.prototype.update = function (time) {
    Player.prototype.update.call(this, time)
    this.mapping.update()
    this.ship.orders["left"]     = this.mapping.value("left")
    this.ship.orders["right"]    = this.mapping.value("right")
    this.ship.orders["forward"]  = this.mapping.value("forward")
    this.ship.orders["reverse"]  = this.mapping.value("reverse")
    this.ship.orders["rotation"] = this.mapping.value("rotation")
    this.ship.orders["fire"]     = this.mapping.value("fire")
}

Test

Mocha: npm test