This repository was archived by the owner on Sep 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
90 lines (78 loc) · 2.08 KB
/
index.js
File metadata and controls
90 lines (78 loc) · 2.08 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* eslint-disable linebreak-style */
const q = require('daskeyboard-applet');
const cmk = require('control-modifier-keys');
const { logger } = q;
class KeyState extends q.DesktopApp {
constructor() {
super();
KeyState.log('CTOR');
this.keys = {
NumLock: {
code: 'numlock',
display: 'Num Lock',
},
CapsLock: {
code: 'capslock',
display: 'Caps Lock',
},
ScrollLock: {
code: 'scrolllock',
display: 'Scroll Lock',
},
};
KeyState.log('Key State ready to go!');
}
// eslint-disable-next-line class-methods-use-this
async run() {
KeyState.log('Running...');
this.pollingInterval = this.config.pollingInterval || 1000;
this.colors = {
on: this.config.colorOn || '#FF0000',
off: this.config.colorOff || '#00FF00',
};
const key = this.config.key || this.keys.CapsLock.code;
try {
const enabled = cmk.getModifierState(key);
KeyState.log(`${key} is ${enabled ? 'enabled' : 'disabled'}`);
return new q.Signal({
points: [
[this.generatePoint(enabled)],
],
name: `${key} State`,
message: `${key} is ${enabled ? 'enabled' : 'disabled'}`,
});
} catch (err) {
KeyState.log(err, `error - ${err}`);
throw new Error(err);
}
}
// eslint-disable-next-line class-methods-use-this
async options(questionKey) {
switch (questionKey) {
case 'key':
return Object.values(this.keys).map(val => ({
key: val.code,
value: val.display,
}));
default:
throw new Error(`Unsupported questionKey: ${questionKey}`);
}
}
generatePoint(on) {
return new q.Point(on ? this.colors.on : this.colors.off);
}
static log(msg, level) {
const prefix = '[KeyState]';
const prefixedMessage = `${prefix} ${msg}`;
if (level === 'error') {
logger.error(prefixedMessage);
} else {
logger.info(prefixedMessage);
}
}
}
module.exports = {
KeyState,
};
// eslint-disable-next-line no-unused-vars
const keyState = new KeyState();