node-hid-input
Node.js binding for reading text inputs on Linux, such as barcode scanners.
Why another...?
Features:
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 devicename
- 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
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