Giter Club home page Giter Club logo

input-device-handler's Introduction

input-device-handler

Handles keyboard, mouse, and gamepad inputs with a single API that treats inputs from all those devices identically so they can be used interchangeably. Also optionally converts the Gamepad API's polling paradigm into an event based paradigm.

Usage

This details the most commonly used parts of teh API. For full API details, use the TypeScript types included in this package and in the GitHub repo.

Install

npm i input-device-handler

Basic usage

Since the gamepad api is poll based (their value must constantly be checked), a new class instance must be constructed so we can listen to events from it.

import {CurrentInputsChangedEvent, InputDeviceHandler, NewDevicesAddedEvent} from 'input-device-handler';

const deviceHandler = new InputDeviceHandler({startLoopImmediately: true});

// listen to new devices connecting (such as gamepads)
deviceHandler.listen(NewDevicesAddedEvent, (event) => {
    console.info('new devices added:', event.detail.inputs);
});

// listen to a change in inputs
deviceHandler.listen(CurrentInputsChangedEvent, (event) => {
    console.info('inputs changed:', event.detail.inputs);
});

Loop control

Under the hood, InputDeviceHandler is hooking into the render loop to constantly poll the gamepads. InputDeviceHandler provides means of controlling this polling loop:

import {InputDeviceHandler} from 'input-device-handler';

const deviceHandler = new InputDeviceHandler();

// start the polling loop, events will get fired now
deviceHandler.startPollingLoop();

// pause the polling loop, events will no longer get fired
deviceHandler.pausePollingLoop();

Usage within a render loop

If you already have a render loop, you can instruct InputDeviceHandler to not start its own poll loop. You can then fire its update method to get all the same functionality but contained within your own render loop.

import {InputDeviceHandler} from 'input-device-handler';

const deviceHandler = new InputDeviceHandler();

function myRenderLoop() {
    const currentDevices = deviceHandler.readAllDevices();
    // do something with the current devices and their inputs...
    requestAnimationFrame(myRenderLoop);
}

myRenderLoop();

input-device-handler's People

Contributors

electrovir avatar

Watchers

James Cloos avatar  avatar

input-device-handler's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.