-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathButtonControl.js
More file actions
33 lines (29 loc) · 938 Bytes
/
ButtonControl.js
File metadata and controls
33 lines (29 loc) · 938 Bytes
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
"use strict";
class ButtonControl {
constructor(device, pollIntervalMs = 100) {
if (process.platform !== 'win32') {
console.warn(`Button control has only been tested on Windows and probably won't work on ${process.platform}`);
}
this.device = device;
this.device.readButtonState(); // clear the event counter so we don't count any presses prior to creation of this object
this.callbacks = [];
this.timer = setInterval(() => {
var that = this;
if (that.callbacks.length > 0) {
var result = that.device.readButtonState();
if (result && result.length > 0 && result[0] > 0) {
// The button was pressed
that.callbacks.forEach(cb => cb(result));
}
}
}, pollIntervalMs);
}
close() {
this.callbacks = [];
clearInterval(this.timer);
}
addButtonPressListener(cb) {
this.callbacks.push(cb);
}
}
module.exports = ButtonControl