0.1.1 • Published 3 years ago
hid-input v0.1.1
node-hid-input
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-inputBuild from source
npm install --save hid-input --build-from-sourceAdditional build dependencies are required on different platforms:
Debian/Ubuntu
sudo apt install -y build-essential cmake ninja-buildmacOS
brew install cmake ninjaUsage
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-stringPath to the devicename-stringHuman readable name of the deivce
List HID input devices available on this machine.
createInput(path[, options])
path-stringoptions-Objectexclusive-booleanWhether 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-stringInput read from the device
close()
Close the stream and release any underlying resources.