Linux uinput nodejs wrapper
npm install uinput
const UInput = require('uinput2');
const SETUP_OPTIONS = {
UI_SET_EVBIT: [
UInput.EV_KEY,
UInput.EV_SYN,
UInput.EV_REL,
],
UI_SET_RELBIT: [
UInput.REL_X,
UInput.REL_Y,
UInput.REL_WHEEL,
UInput.REL_HWHEEL,
],
UI_SET_KEYBIT: [
UInput.BTN_LEFT,
UInput.BTN_RIGHT,
UInput.BTN_MIDDLE,
UInput.BTN_SIDE,
UInput.BTN_EXTRA,
],
};
const CREATE_OPTIONS = {
name: 'Wine Launcher Mouse Emulation',
id: {
busType: UInput.BUS_USB,
vendor: 0x0,
product: 0x0,
version: 1
},
};
export default class UInputMouse {
static map = {
left: UInput.BTN_LEFT,
right: UInput.BTN_RIGHT,
middle: UInput.BTN_MIDDLE,
};
runtime = Promise.resolve();
delay = 2;
device;
async createDevice() {
if (undefined !== this.device) {
return this.device;
}
this.device = await UInput.setup(SETUP_OPTIONS);
this.device.create(CREATE_OPTIONS);
return this.device;
}
async sleep(time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
/**
* @param {number} x
* @param {number} y
*/
moveMouse(x, y) {
this.createDevice().then(device => {
this.runtime = this.runtime
.then(() => device.sendEvent(UInput.EV_REL, UInput.REL_X, x, false))
.then(() => device.sendEvent(UInput.EV_REL, UInput.REL_Y, y, true))
.then(() => this.sleep(this.delay));
});
}
/**
* @param {string} key [left, right, middle]
* @param {boolean} pressed
*/
keyToggle(key, pressed) {
if (undefined === UInputMouse.map[key]) {
return;
}
this.createDevice().then(device => {
this.runtime = this.runtime
.then(() => device.keyEvent(UInputMouse.map[key], pressed))
.then(() => this.sleep(this.delay));
});
}
}
const device = new UInputMouse();
device.moveMouse(0, 100);
device.keyToggle('right', true);
device.keyToggle('right', false);
- options
Object
- event_type where event_type can be
EV_KEY
,EV_ABS
,EV_REL
, etc. and it's anArray
with the different events we want the uinput device to handle
- event_type where event_type can be
- Returns a UInputClass on success
It configures the uinput device we are about to create.
- options
Object
. Seeuinput_user_dev
definition in linux/uinput.h- name
String
with the name of the device - id
Object
- busType
Number
- vendor
Number
- product
Number
- version
Number
- busType
- ffEffectsMax
Number
- absMax
Array
ofNumbers
of size:UInput.ABS_CNT
- absMin
Array
ofNumbers
of size:UInput.ABS_CNT
- absFuzz
Array
ofNumbers
of size:UInput.ABS_CNT
- absFlat
Array
ofNumbers
of size:UInput.ABS_CNT
- name
It creates the uinput device.
- type
Number
- code
Number
- value
Number
- syn
Boolean
It sends an event to the uinput device.
- code
Number
- press
Boolean
Wrapper over send_event to simulate key presses and mouse clicks.
- code
Array with any combination of keys
It sends an event to the uinput device with the combination keys generated.