hid-input
v0.1.1
Published
Node.js binding for reading text inputs on Linux, such as barcode scanners
Downloads
5
Readme
node-hid-input
Node.js binding for reading text inputs on Linux, such as barcode scanners.
Why another...?
Features:
- [x] First class TypeScript support
- [x] Node-API addon for better parsing of
input_event
- [x] Supports exclusive (
EVIOCGRAB
) open - [x] 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 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
- 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.