Skip to content

Node.js binding for reading text inputs on Linux, such as barcode scanners

License

Notifications You must be signed in to change notification settings

xingrz/node-hid-input

Repository files navigation

node-hid-input

license issues stars commits

Node.js binding for reading text inputs on Linux, such as barcode scanners.

Why another...?

Features:

  • First class TypeScript support
  • Node-API addon for better parsing of input_event
  • Supports exclusive (EVIOCGRAB) open
  • Mocking interface for better unit testing, even on platforms other than Linux

Installation

npm install --save hid-input

Build from source

npm install --save hid-input --build-from-source

Additional build dependencies are required on different platforms:

Debian/Ubuntu

sudo apt install -y build-essential cmake ninja-build

macOS

brew install cmake ninja

Usage

import { createInput, listInputs } from 'hid-input';

const inputs = await listInputs();
console.log(inputs);

const reader = createInput(inputs[0].path);
reader.once('open', ()=>{
  console.log('device opened');
});
reader.on('data', (input) => {
  console.log(`- scanned: ${input}`);
});

APIs

listInputs()

  • Returns: Promise<InputDevice[]>
    • path - string Path to the device
    • name - string Human readable name of the deivce

List HID input devices available on this machine.

createInput(path[, options])

  • path - string
  • options - Object
    • exclusive - boolean Whether to prevent the device from being opened by other processes before getting closed
  • Returns: HidInputStream

Opens an input stream of device path. Available devices along with paths can be retrieved with listInputs().

createMockInput(path)

  • path - string
  • Returns: HidInputStream

Opens a mocking stream by listening on a newly created UNIX domain socket path.

After the stream is open, you can emulate inputs with nc -U <path>.

Class: HidInputStream

  • Extends fs.Readable

A readable stream in Object mode.

Event: open

Event: close

Event: data

  • input - string Input read from the device

close()

Close the stream and release any underlying resources.

License

MIT License

About

Node.js binding for reading text inputs on Linux, such as barcode scanners

Resources

License

Stars

Watchers

Forks