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

hid-handler

v0.0.8

Published

USB Hid handler for nodejs

Downloads

4

Readme

NPM version Build Status Coverage Status

NPM

USB HID Handler

This nodejs module provides an event handler for USB HID devices.

NodeJS Mouse Keyboard Numpad Touchpad Presenter

Based on libusb, thx to the wonderful node-usb nodejs module.

The purpose of this module is to provide a higher level of USB devices handling, and just use it in your app without having to deal with all technical details of libusb.

Installation

$ npm install hid-handler

Quick start

Suppose you want to handle an USB keyboard :

var hidHandler = require('hid-handler');

hidHandler.on('key', function (event) {
  console.log('keycodes :', event.keycodes); // raw keycodes array emitted by the keyboard
});

hidHandler.start({
  supportedDevices: {                           // object or array of devices
    name: 'Microsoft comfort curve keyboard',   // optional (not used decoration)
    type: 'keyboard',                           // if not specified, only generic events are emitted
    vendorId: 0x045e,                           // required
    productId: 0x00dd                           // required
  }
});

process.on('SIGINT', function () { // properly stop the handler in a simple main app
  hidHandler.stop();
});

Usage

Hid handler first registers the devices you want, or all the detected ones.

After starting, every registered devices are mapped to an event handler class (see event-handlers).

Each event handler class is matching a type of device (keyboard, mouse, ...).

Hid handler exposes an event emitter to manage all type of events.

Types of event :

  • 'event' : any event of any device (fallback)
  • 'key' : a key is pressed and released
  • 'move' : mouse (or pad) move (left, right, up, down)
  • 'click' : mouse (or pad) click (left, right, ...)
  • 'wheel' : mouse wheel click (up, down)

Example of usage :

var hidHandler = require('hid-handler');

hidHandler.on('event', function (event) { // catch all events for all devices
  console.log('event :', event.toString()); // event class toString to display nice events status
  console.log('device :', event.hid.deviceKey.toString()); // hid device key concerned
});

hidHandler.on('key', function (event) { // catch key events (emitted by keyboard devices)
  console.log('key event :', event.toString());
});

hidHandler.on('click', function (event) { // catch click events (emitted by mouse or pad devices)
  console.log('click event :', event.toString());
});

hidHandler.on('wheel', function (event) { // catch wheel events (emitted by mouse devices)
  console.log('wheel event :', event.toString());
});

hidHandler.on('move', function (event) { // catch move events (emitted by mouse devices)
  console.log('move event :', event.toString());
});

hidHandler.init({
  supportedDevices: [{
    name: 'Razer mouse',    // optional
    type: { 0: 'mouse' },  // optional, types of interfaces (defaults : {0:generic})
    vendorId: 0x1532,       // required
    productId: 0x0003,      // required
  }]
});

process.on('SIGINT', function () { // properly stop the handler in a simple main app
  hidHandler.stop();
});

hidHandler.start(function() {
  console.log('started');
}); // starts the handler

Device types

Currently supported device types are :

  • generic : used for base class or unknown devices
  • keyboard : generates key pressed events
  • numpad : generates key pressed events
  • mouse : generates clicks, wheel clicks and move events
  • touchpad : generates clicks and move events
  • presenter : generates clicks, move and key events

The 'type' property of a supported device should match available event handler class names to lower case and without the word 'Event'. (example : 'KeyboardEvent' class matches type 'keyboard')

If type is an object each key should be an interface index (0, 1, ...) else if type is a string it defines only the type of the first interface (0).

Available default values for 'type' are : keyboard, numpad, mouse, touchpad, generic (defaults).

Event handler classes are extendable with registerEventHandler.

Feel free to extend to map your needs !

API

init(opt)

Initialize hid handler with optional opt.supportedDevices to specify devices to handle (by default all connected devices are handled).

supportedDevices should be an array of objects containing vendorId and productId properties.

To find vendorId and productId of your existing devices, check bin/scandevices :-)

Options :

{
  supportedDevices,     // array of objects containing vendorId and productId properties to support
  keyLayouts: {
    baseDir,            // custom base dir to scan for keyboard layouts
    layouts             // extra custom keyboard layouts
  }
}

isStarted()

Returns true if hid handler is started, false else.

start(opt, cb)

Starts hid handler and return a promise (or call the cb callback).

First call init(opt) if it was not done before.

If opt is provided and init was not called before, then start will call.

Warning : all handled devices will be detached from the kernel and reattached to it when stop() is called

stop

Stops hid handler, and free all resources :-)

getSupportedDevice(vendorId, productId)

Find and return the matching device from supported devices list.

getRegisteredDevices

Returns list of registered devices.

Each device is an object containing :

  • deviceKey: vendorId:productId key
  • product: id of the product
  • manufacturer : id of the manufacturer

getRegisteredHid(vendorId, productId)

Returns the registered hid matching vendorId and productId, or null if not found.

getRegisteredHids

Returns the registered hids.

getRegisteredHidKeys

Returns the registered hid keys (vendorId:productId).

getSupportedDevice(vendorId, productId)

Returns supported device matching vendorId and productId, or null if not found.

The supported devices are specified at init() invocation, by default all connected devices are supported.

registerEventHandler(eventHandlerClass)

Registers an event handler class. Useful to add a custom event handler for a device that's not supported.

keyLayouts

Provides keyboard layouts to convert scan codes to keycodes.

If a layout matches the registered device, events are populated with the associated keycode in addition to scan codes.

Layouts are provided as properties, json or yaml files.

Hid handler includes some default layouts (generic qwerty, azerty-fr).

Feel free to add some custom layouts with :

  • registerLayout(layout) : register an extra layout
  • registerLayoutFile(file) : register a layout file
  • registerLayoutDir(dir) : scan layout files from specific directory

Layout format

  • name : name of the layout (for a file this is the basename of the file)
  • value : an object containing scancodes as keys, and keycodes as values

Keycodes values can be arrays to manage modifiers (shift, alt, ctrl)

Keycodes with spaces are considered as arrays

Extra layouts values always inherit from generic layout.

Example of layout file generic.properties

util

Helper that overrides NodesJS util.

Event handler class

The GenericEvent class is the base class used as a super class by the others.

An event handler class should :

  • have a name matching [A-Z].*Event
  • provide a constructor that calls super constructor
  • optionally provide toString() method
  • provide parseData() method to parse data emitted by the matching device
  • provide emit(sources) method to emit high level events to the world

Example of event handler class implementation : KeyboardEvent

Enjoy!