All Projects → node-hid → Node Hid

node-hid / Node Hid

Access USB & Bluetooth HID devices through Node.js

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Node Hid

ESP32-USB-Soft-Host
An Arduino wrapper to @sdima1357's usb_soft_host esp-idf example
Stars: ✭ 119 (-88.82%)
Mutual labels:  usb, hid
Teenyusb
Lightweight USB device and host stack for STM32 and other MCUs.
Stars: ✭ 287 (-73.03%)
Mutual labels:  usb, hid
RaspberryPi-Joystick
A virtual HID USB joystick created using Raspberry Pi
Stars: ✭ 73 (-93.14%)
Mutual labels:  usb, hid
ES-Timer
A USB timer powered by Digispark ATtiny85 according to 🍅 pomodoro time management technique
Stars: ✭ 45 (-95.77%)
Mutual labels:  usb, hid
Hidapi
A Simple library for communicating with USB and Bluetooth HID devices on Linux, Mac and Windows.
Stars: ✭ 465 (-56.3%)
Mutual labels:  usb, hid
pi400kb
Raw HID keyboard forwarder to turn the Pi 400 into a USB keyboard
Stars: ✭ 182 (-82.89%)
Mutual labels:  usb, hid
usb stack
Tiny and portable USB device/host stack for embedded system with USB IP
Stars: ✭ 175 (-83.55%)
Mutual labels:  usb, hid
Usbdevice
Highly flexible Composite USB Device Library
Stars: ✭ 144 (-86.47%)
Mutual labels:  usb, hid
Headsetcontrol
Sidetone and Battery status for Logitech G930, G533, G633, G933 SteelSeries Arctis 7/PRO 2019 and Corsair VOID (Pro) in Linux and MacOSX
Stars: ✭ 392 (-63.16%)
Mutual labels:  usb, hid
Node Elgato Stream Deck
A Node.js library for interfacing with the Elgato Stream Deck.
Stars: ✭ 359 (-66.26%)
Mutual labels:  usb, hid
python-hid-parser
Typed pure Python library to parse HID report descriptors
Stars: ✭ 31 (-97.09%)
Mutual labels:  usb, hid
Vigembus
Windows kernel-mode driver emulating well-known USB game controllers.
Stars: ✭ 721 (-32.24%)
Mutual labels:  usb, hid
EspTinyUSB
ESP32S2 native USB library. Implemented few common classes, like MIDI, CDC, HID or DFU (update).
Stars: ✭ 375 (-64.76%)
Mutual labels:  usb, hid
Hidpytoy
A GUI app for playing with HID devices, written in Python
Stars: ✭ 25 (-97.65%)
Mutual labels:  usb, hid
Authorizer
Authorizer is a Password Manager for Android. It emulates an HID keyboard over USB and enters your credentials on your target device. Additionally it supports OTP 🔑📴
Stars: ✭ 172 (-83.83%)
Mutual labels:  usb, hid
android-usb-script
An Android app that allows you to script USB gadgets (work-in-progress).
Stars: ✭ 15 (-98.59%)
Mutual labels:  usb, hid
Android Usb Gadget
Convert your Android phone to any USB device you like! USB Gadget Tool allows you to create and activate USB device roles, like a mouse or a keyboard. 🛠🛡📱
Stars: ✭ 118 (-88.91%)
Mutual labels:  usb, hid
Hidguardian
Windows kernel-mode driver for controlling access to various input devices.
Stars: ✭ 138 (-87.03%)
Mutual labels:  usb, hid
Device.net
A C# cross platform connected device framework
Stars: ✭ 347 (-67.39%)
Mutual labels:  usb, hid
Hidviz
A tool for in-depth analysis of USB HID devices communication
Stars: ✭ 505 (-52.54%)
Mutual labels:  usb, hid

node-hid - Access USB HID devices from Node.js

npm build macos build windows build linux

Platform Support

node-hid supports Node.js v6 and upwards. For versions before that, you will need to build from source. The platforms, architectures and node versions node-hid supports are the following. In general we try to provide pre-built native library binaries for the most common platforms, Node and Electron versions.

We strive to make node-hid cross-platform so there's a good chance any combination not listed here will compile and work.

Supported Platforms

  • Windows x86 (32-bit) (¹)
  • Windows x64 (64-bit)
  • Mac OSX 10.9+
  • Linux x64 (²)
  • Linux x86 (¹)
  • Linux ARM / Raspberry Pi (¹)
  • Linux MIPSel (¹)
  • Linux PPC64 (¹)

¹ prebuilt-binaries not provided for these platforms
² prebuilt binary built on Ubuntu 18.04 x64

Supported Node versions

  • Node v8 to
  • Node v14

Supported Electron versions

  • Electron v3 to
  • Electron v11

Any newer version of Electron or Node MAY NOT WORK

Native modules like node-hid require upstream dependencies to be updated to work with newer Node and Electron versions. Unless you need the features in the most recent Electron or Node, use a supported version.

Installation

For most "standard" use cases (node v4.x on mac, linux, windows on a x86 or x64 processor), node-hid will install like a standard npm package:

npm install node-hid

If you install globally, the test program src/show-devices.js is installed as hid-showdevices. On Linux you can use it to try the difference between hidraw and libusb driverTypes:

$ npm install -g node-hid
$ hid-showdevices libusb
$ hid-showdevices hidraw

Installation Special Cases

We are using prebuild to compile and post binaries of the library for most common use cases (Linux, MacOS, Windows on standard processor platforms). If a prebuild is not available, node-hid will work, but npm install node-hid will compile the binary when you install. For more details on compiler setup, see Compling from source below.

Examples

In the src/ directory, various JavaScript programs can be found that talk to specific devices in some way. Some interesting ones:

To try them out, run them with node src/showdevices.js from within the node-hid directory.


Usage

List all HID devices connected

var HID = require('node-hid');
var devices = HID.devices();

devices will contain an array of objects, one for each HID device available. Of particular interest are the vendorId and productId, as they uniquely identify a device, and the path, which is needed to open a particular device.

Sample output:

HID.devices();
{ vendorId: 10168,
    productId: 493,
    path: 'IOService:/[email protected],0',
    serialNumber: '20002E8C',
    manufacturer: 'ThingM',
    product: 'blink(1) mk2',
    release: 2,
    interface: -1,
    usagePage: 65280,
    usage: 1 },
  { vendorId: 1452,
    productId: 610,
    path: 'IOService:/[email protected],0',
    serialNumber: '',
    manufacturer: 'Apple Inc.',
    product: 'Apple Internal Keyboard / Trackpad',
    release: 549,
    interface: -1,
    usagePage: 1,
    usage: 6 },
    <and more>

Cost of HID.devices() and new HID.HID() for detecting device plug/unplug

Both HID.devices() and new HID.HID() are relatively costly, each causing a USB (and potentially Bluetooth) enumeration. This takes time and OS resources. Doing either can slow down the read/write that you do in parallel with a device, and cause other USB devices to slow down too. This is how USB works.

If you are polling HID.devices() or doing repeated new HID.HID(vid,pid) to detect device plug / unplug, consider instead using node-usb-detection. node-usb-detection uses OS-specific, non-bus enumeration ways to detect device plug / unplug.

Opening a device

Before a device can be read from or written to, it must be opened. The path can be determined by a prior HID.devices() call. Use either the path from the list returned by a prior call to HID.devices():

var device = new HID.HID(path);

or open the first device matching a VID/PID pair:

var device = new HID.HID(vid,pid);

The device variable will contain a handle to the device. If an error occurs opening the device, an exception will be thrown.

A node-hid device is an EventEmitter. While it shares some method names and usage patterns with Readable and Writable streams, it is not a stream and the semantics vary. For example, device.write does not take encoding or callback args and device.pause does not do the same thing as readable.pause. There is also no pipe method.

Picking a device from the device list

If you need to filter down the HID.devices() list, you can use standard Javascript array techniques:

var devices = HID.devices();
var deviceInfo = devices.find( function(d) {
    var isTeensy = d.vendorId===0x16C0 && d.productId===0x0486;
    return isTeensy && d.usagePage===0xFFAB && d.usage===0x200;
});
if( deviceInfo ) {
  var device = new HID.HID( deviceInfo.path );
  // ... use device
}

Reading from a device

To receive FEATURE reports, use device.getFeatureReport().

To receive INPUT reports, use device.on("data",...). A node-hid device is an EventEmitter. Reading from a device is performed by registering a "data" event handler:

device.on("data", function(data) {});

You can also listen for errors like this:

device.on("error", function(err) {});

For FEATURE reports:

var buf = device.getFeatureReport(reportId, reportLength)

Notes:

  • Reads via device.on("data") are asynchronous
  • Reads via device.getFeatureReport() are synchronous
  • To remove an event handler, close the device with device.close()
  • When there is not yet a data handler or no data handler exists, data is not read at all -- there is no buffer.

Writing to a device

To send FEATURE reports, use device.sendFeatureReport().

To send OUTPUT reports, use device.write().

All writing is synchronous.

The ReportId is the first byte of the array sent to device.sendFeatureReport() or device.write(), meaning the array should be one byte bigger than your report. If your device does NOT use numbered reports, set the first byte of the 0x00.

device.write([0x00, 0x01, 0x01, 0x05, 0xff, 0xff]);
device.sendFeatureReport( [0x01, 'c', 0, 0xff,0x33,0x00, 70,0, 0] );

Notes:

  • You must send the exact number of bytes for your chosen OUTPUT or FEATURE report.
  • Both device.write() and device.sendFeatureReport() return number of bytes written + 1.
  • For devices using Report Ids, the first byte of the array to write() or sendFeatureReport() must be the Report Id.

Complete API

devices = HID.devices()

  • Return array listing all connected HID devices

HID.setDriverType(type)

  • Linux only
  • Sets underlying HID driver type
  • type can be "hidraw" or "libusb", defaults to "hidraw"

device = new HID.HID(path)

  • Open a HID device at the specified platform-specific path

device = new HID.HID(vid,pid)

  • Open first HID device with specific VendorId and ProductId

device.on('data', function(data) {} )

  • data - Buffer - the data read from the device

device.on('error, function(error) {} )

  • error - The error Object emitted

device.write(data)

  • data - the data to be synchronously written to the device, first byte is Report Id or 0x00 if not using numbered reports.
  • Returns number of bytes actually written

device.close()

  • Closes the device. Subsequent reads will raise an error.

device.pause()

  • Pauses reading and the emission of data events.
    This means the underlying device is silenced until resumption -- it is not like pausing a stream, where data continues to accumulate.

device.resume()

  • This method will cause the HID device to resume emmitting data events. If no listeners are registered for the data event, data will be lost.

  • When a data event is registered for this HID device, this method will be automatically called.

device.read(callback)

  • Low-level function call to initiate an asynchronous read from the device.
  • callback is of the form callback(err, data)

device.readSync()

  • Return an array of numbers data. If an error occurs, an exception will be thrown.

device.readTimeout(time_out)

  • time_out - timeout in milliseconds
  • Return an array of numbers data. If an error occurs, an exception will be thrown.

device.sendFeatureReport(data)

  • data - data of HID feature report, with 0th byte being report_id ([report_id,...])
  • Returns number of bytes actually written

device.getFeatureReport(report_id, report_length)

  • report_id - HID feature report id to get
  • report_length - length of report

device.setNonBlocking(no_block)

  • no_block - boolean. Set to true to enable non-blocking reads
  • exactly mirrors hid_set_nonblocking() in hidapi

General notes:

Thread safety, Worker threads, Context-aware modules

In general node-hid is not thread-safe because the underlying C-library it wraps (hidapi) is not thread-safe. However, node-hid is now reporting as minimally Context Aware to allow use in Electron v9+. Until node-hid (or hidapi) is rewritten to be thread-safe, please constrain all accesses to it via a single thread.

Devices node-hid cannot read

The following devices are unavailable to node-hid because the OS owns them:

  • Keyboards
  • Mice
  • Barcode readers (in USB HID keyboard mode)
  • RFID scanners (in USB HID keyboard mode)
  • Postage Scales (in USB HID keyboard mode)

Most OSes will prevent USB HID keyboards or mice, or devices that appear as a keyboard to the OS. This includes many RFID scanners, barcode readers, USB HID scales, and many other devices. This is a security precaution. Otherwise, it would be trivial to build keyloggers.

Some keyboard-pretending devices like barcode or RFID readers can be configured to be in "HID data" mode or "Serial / UART" mode. If in "HID Data" mode then node-hid can access them, if in "Serial / UART" mode, you should use node-serialport instead.

Mac notes

See General notes above Keyboards

Windows notes

See General notes above about Keyboards

Xbox 360 Controller on Windows 10

For reasons similar to mice & keyboards it appears you can't access this controller on Windows 10.

Linux notes

See General notes above about Keyboards

udev device permissions

Most Linux distros use udev to manage access to physical devices, and USB HID devices are normally owned by the root user. To allow non-root access, you must create a udev rule for the device, based on the devices vendorId and productId.

This rule is a text file placed in /etc/udev/rules.d.

For an example HID device (say a blink(1) light with vendorId = 0x27b8 and productId = 0x01ed, the rules file to support both hidraw and libusb would look like:

SUBSYSTEM=="input", GROUP="input", MODE="0666"
SUBSYSTEM=="usb", ATTRS{idVendor}=="27b8", ATTRS{idProduct}=="01ed", MODE:="666", GROUP="plugdev"
KERNEL=="hidraw*", ATTRS{idVendor}=="27b8", ATTRS{idProduct}=="01ed", MODE="0666", GROUP="plugdev"

Note that the values for idVendor and idProduct must be in hex and lower-case.

Save this file as /etc/udev/rules.d/51-blink1.rules, unplug the HID device, and reload the rules with:

sudo udevadm control --reload-rules

For a complete example, see the blink1 udev rules.

Selecting driver type

By default as of [email protected], the hidraw driver is used to talk to HID devices. Before [email protected], the more older but less capable libusb driver was used. With hidraw Linux apps can now see usage and usagePage attributes of devices.

If you would still like to use the libusb driver, then you can do either:

During runtime, you can use HID.setDriverType('libusb') immediately after require()-ing node-hid:

var HID = require('node-hid');
HID.setDriverType('libusb');

If you must have the libusb version and cannot use setDriverType(), you can install older node-hid or build from source:

npm install [email protected]

or:

npm install node-hid --build-from-source --driver=libusb

Compiling from source

To compile & develop locally or if prebuild cannot download a pre-built binary for you, you will need the following compiler tools and libraries:

Linux (kernel 2.6+) : (install examples shown for Debian/Ubuntu)

  • Compilation tools: apt install build-essential git
  • gcc-4.8+: apt install gcc-4.8 g++-4.8 && export CXX=g++-4.8
  • libusb-1.0-0 w/headers:apt install libusb-1.0-0 libusb-1.0-0-dev
  • libudev-dev: apt install libudev-dev (Debian/Ubuntu) / yum install libusbx-devel (Fedora)

FreeBSD

  • Compilation tools: pkg install git gcc gmake libiconv node npm

Mac OS X 10.8+

Windows 7, 8, 10

  • Visual C++ compiler and Python 2.7
    • either:
      • npm install --global windows-build-tools
      • add %USERPROFILE%\.windows-build-tools\python27 to PATH, like PowerShell: $env:Path += ";$env:USERPROFILE\.windows-build-tools\python27"
    • or:

Building node-hid from source, for your projects

npm install node-hid --build-from-source

Build node-hid for node-hid development

  • check out a copy of this repo
  • change into its directory
  • update the submodules
  • build the node package

For example:

git clone https://github.com/node-hid/node-hid.git
cd node-hid                                        # must change into node-hid directory
npm install -g rimraf                              # just so it doesn't get 'clean'ed
npm run prepublishOnly                             # get the needed hidapi submodule
npm install --build-from-source                    # rebuilds the module with C code
npm run showdevices                                # list connected HID devices
node ./src/show-devices.js                         # same as above

You may see some warnings from the C compiler as it compiles hidapi (the underlying C library node-hid uses).
This is expected.

For ease of development, there are also the scripts:

npm run gypclean      # "node-gyp clean" clean gyp build directory
npm run gypconfigure  # "node-gyp configure" configure makefiles
npm run gypbuild      # "node-gyp build" build native code

Building node-hid for cross-compiling

When cross-compiling you need to override node-hid's normal behavior of using Linux pkg-config to determine CLFAGS and LDFLAGS for libusb. To do this, you can use the node-gyp variable node_hid_no_pkg_config and then invoke a node-hid rebuild with either:

  node-gyp rebuild --node_hid_no_pkg_config=1

or

  npm gyprebuild --node_hid_no_pkg_config=1

Electron projects using node-hid

In your electron project, add electron-rebuild to your devDependencies. Then in your package.json scripts add:

  "postinstall": "electron-rebuild"

This will cause npm to rebuild node-hid for the version of Node that is in Electron. If you get an error similar to The module "HID.node" was compiled against a different version of Node.js then electron-rebuild hasn't been run and Electron is trying to use node-hid compiled for Node.js and not for Electron.

If using node-hid with webpack or similar bundler, you may need to exclude node-hid and other libraries with native code. In webpack, you say which externals you have in your webpack-config.js:

  externals: {
    "node-hid": 'commonjs node-hid'
  }

Examples of node-hid in Electron:

NW.js projects using node-hid

Without knowing much about NW.js, a quick hacky solution that works is:

cd my-nwjs-app
npm install node-hid --save
npm install -g nw-gyp
cd node_modules/node-hid
nw-gyp rebuild --target=0.42.3 --arch=x64  // or whatever NW.js version you have
cd ../..
nwjs .

Support

Please use the node-hid github issues page for support questions and issues.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].