-
Notifications
You must be signed in to change notification settings - Fork 5
/
keyboard.js
56 lines (45 loc) · 2.1 KB
/
keyboard.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// IMPORTANT! This needs to be run as Administrator
// Imports
const HID = require("node-hid");
// Constants
const VENDOR_ID = 0x0c45;
const PRODUCT_ID = 0x5004;
const RELEASE = 0x107;
const INTERFACE = 0x1;
const USAGE_PAGE = 0xFF1C;
// Configuration
HID.setDriverType("libusb");
// Utility methods
function enumerateDevices() {
return HID.devices()
.filter(x => x.vendorId === VENDOR_ID)
.filter(x => x.productId === PRODUCT_ID)
.filter(x => x.release === RELEASE)
.filter(x => x.interface === INTERFACE)
.filter(x => x.usagePage === USAGE_PAGE)
.filter(x => /USB DEVICE/gi.test(x.product))
.filter(x => /sonix/gi.test(x.manufacturer));
}
function sendChangeColorCommand(device, { red, green, blue }) {
device.write([0x04, 0x0c, 0x02, 0x06, 0x03, 0x05, 0x00, 0x00, red, green, blue, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
}
function sendApplyChangesCommand(device) {
device.write([0x04, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]);
}
function openDevice(deviceInfo, color) {
try {
let device = new HID.HID(deviceInfo.path);
sendChangeColorCommand(device, color);
sendApplyChangesCommand(device);
device.close();
} catch (error) {
console.error(error);
}
}
// Export
module.exports = function (color) {
// List all hid devices that match the specified filter
let devices = enumerateDevices();
// For each device, open it and send the commands
devices.forEach(x => openDevice(x, color));
};