-
Notifications
You must be signed in to change notification settings - Fork 291
/
index.js
414 lines (358 loc) · 10.7 KB
/
index.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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
const EventEmitter = require('events');
const path = require('path');
const runtime = process.versions['electron'] ? 'electron' : 'node';
const essential =
runtime +
'-v' +
process.versions.modules +
'-' +
process.platform +
'-' +
process.arch;
const modulePath = path.join(
__dirname,
'builds',
essential,
'build',
'Release',
'iohook.node'
);
if (process.env.DEBUG) {
console.info('Loading native binary:', modulePath);
}
let NodeHookAddon = require(modulePath);
const events = {
3: 'keypress',
4: 'keydown',
5: 'keyup',
6: 'mouseclick',
7: 'mousedown',
8: 'mouseup',
9: 'mousemove',
10: 'mousedrag',
11: 'mousewheel',
};
class IOHook extends EventEmitter {
constructor() {
super();
this.active = false;
this.shortcuts = [];
this.eventProperty = 'keycode';
this.activatedShortcuts = [];
this.lastKeydownShift = false;
this.lastKeydownAlt = false;
this.lastKeydownCtrl = false;
this.lastKeydownMeta = false;
this.load();
this.setDebug(false);
}
/**
* Start hook process
* @param {boolean} [enableLogger] Turn on debug logging
*/
start(enableLogger) {
if (!this.active) {
this.active = true;
this.setDebug(enableLogger);
}
}
/**
* Shutdown event hook
*/
stop() {
if (this.active) {
this.active = false;
}
}
/**
* Register global shortcut. When all keys in keys array pressed, callback will be called
* @param {Array} keys Array of keycodes
* @param {Function} callback Callback for when shortcut pressed
* @param {Function} [releaseCallback] Callback for when shortcut has been released
* @return {number} ShortcutId for unregister
*/
registerShortcut(keys, callback, releaseCallback) {
let shortcut = {};
let shortcutId = Date.now() + Math.random();
keys.forEach((keyCode) => {
shortcut[keyCode] = false;
});
shortcut.id = shortcutId;
shortcut.callback = callback;
shortcut.releaseCallback = releaseCallback;
this.shortcuts.push(shortcut);
return shortcutId;
}
/**
* Unregister shortcut by ShortcutId
* @param shortcutId
*/
unregisterShortcut(shortcutId) {
this.shortcuts.forEach((shortcut, i) => {
if (shortcut.id === shortcutId) {
this.shortcuts.splice(i, 1);
}
});
}
/**
* Unregister shortcut via its key codes
* @param {string} keyCodes Keyboard keys matching the shortcut that should be unregistered
*/
unregisterShortcutByKeys(keyCodes) {
// A traditional loop is used in order to access `this` from inside
for (let i = 0; i < this.shortcuts.length; i++) {
let shortcut = this.shortcuts[i];
// Convert any keycode numbers to strings
keyCodes.forEach((key, index) => {
if (typeof key !== 'string' && !(key instanceof String)) {
// Convert to string
keyCodes[index] = key.toString();
}
});
// Check if this is our shortcut
Object.keys(shortcut).every((key) => {
if (key === 'callback' || key === 'id') return;
// Remove all given keys from keyCodes
// If any are not in this shortcut, then this shortcut does not match
// If at the end we have eliminated all codes in keyCodes, then we have succeeded
let index = keyCodes.indexOf(key);
if (index === -1) return false; // break
// Remove this key from the given keyCodes array
keyCodes.splice(index, 1);
return true;
});
// Is this the shortcut we want to remove?
if (keyCodes.length === 0) {
// Unregister this shortcut
this.shortcuts.splice(i, 1);
return;
}
}
}
/**
* Unregister all shortcuts
*/
unregisterAllShortcuts() {
this.shortcuts.splice(0, this.shortcuts.length);
}
/**
* Load native module
*/
load() {
NodeHookAddon.startHook(this._handler.bind(this), this.debug || false);
}
/**
* Unload native module and stop hook
*/
unload() {
this.stop();
NodeHookAddon.stopHook();
}
/**
* Enable or disable native debug output
* @param {Boolean} mode
*/
setDebug(mode) {
NodeHookAddon.debugEnable(mode);
}
/**
* Specify that key event's `rawcode` property should be used instead of
* `keycode` when listening for key presses.
*
* This allows iohook to be used in conjunction with other programs that may
* only provide a keycode.
* @param {Boolean} using
*/
useRawcode(using) {
// If true, use rawcode, otherwise use keycode
this.eventProperty = using ? 'rawcode' : 'keycode';
}
/**
* Disable mouse click propagation.
* The click event are captured and the event emitted but not propagated to the window.
*/
disableClickPropagation() {
NodeHookAddon.grabMouseClick(true);
}
/**
* Enable mouse click propagation (enabled by default).
* The click event are emitted and propagated.
*/
enableClickPropagation() {
NodeHookAddon.grabMouseClick(false);
}
/**
* Local event handler. Don't use it in your code!
* @param msg Raw event message
* @private
*/
_handler(msg) {
if (this.active === false || !msg) return;
if (events[msg.type]) {
const event = msg.mouse || msg.keyboard || msg.wheel;
event.type = events[msg.type];
this._handleShift(event);
this._handleAlt(event);
this._handleCtrl(event);
this._handleMeta(event);
this.emit(events[msg.type], event);
// If there is any registered shortcuts then handle them.
if (
(event.type === 'keydown' || event.type === 'keyup') &&
iohook.shortcuts.length > 0
) {
this._handleShortcut(event);
}
}
}
/**
* Handles the shift key. Whenever shift is pressed, all future events would
* contain { shiftKey: true } in its object, until the shift key is released.
* @param event Event object
* @private
*/
_handleShift(event) {
if (event.type === 'keyup' && event.shiftKey) {
this.lastKeydownShift = false;
}
if (event.type === 'keydown' && event.shiftKey) {
this.lastKeydownShift = true;
}
if (this.lastKeydownShift) {
event.shiftKey = true;
}
}
/**
* Handles the alt key. Whenever alt is pressed, all future events would
* contain { altKey: true } in its object, until the alt key is released.
* @param event Event object
* @private
*/
_handleAlt(event) {
if (event.type === 'keyup' && event.altKey) {
this.lastKeydownAlt = false;
}
if (event.type === 'keydown' && event.altKey) {
this.lastKeydownAlt = true;
}
if (this.lastKeydownAlt) {
event.altKey = true;
}
}
/**
* Handles the ctrl key. Whenever ctrl is pressed, all future events would
* contain { ctrlKey: true } in its object, until the ctrl key is released.
* @param event Event object
* @private
*/
_handleCtrl(event) {
if (event.type === 'keyup' && event.ctrlKey) {
this.lastKeydownCtrl = false;
}
if (event.type === 'keydown' && event.ctrlKey) {
this.lastKeydownCtrl = true;
}
if (this.lastKeydownCtrl) {
event.ctrlKey = true;
}
}
/**
* Handles the meta key. Whenever meta is pressed, all future events would
* contain { metaKey: true } in its object, until the meta key is released.
* @param event Event object
* @private
*/
_handleMeta(event) {
if (event.type === 'keyup' && event.metaKey) {
this.lastKeydownMeta = false;
}
if (event.type === 'keydown' && event.metaKey) {
this.lastKeydownMeta = true;
}
if (this.lastKeydownMeta) {
event.metaKey = true;
}
}
/**
* Local shortcut event handler
* @param event Event object
* @private
*/
_handleShortcut(event) {
if (this.active === false) {
return;
}
// Keep track of shortcuts that are currently active
let activatedShortcuts = this.activatedShortcuts;
if (event.type === 'keydown') {
this.shortcuts.forEach((shortcut) => {
if (shortcut[event[this.eventProperty]] !== undefined) {
// Mark this key as currently being pressed
shortcut[event[this.eventProperty]] = true;
let keysTmpArray = [];
let callme = true;
// Iterate through each keyboard key in this shortcut
Object.keys(shortcut).forEach((key) => {
if (key === 'callback' || key === 'releaseCallback' || key === 'id')
return;
// If one of the keys aren't pressed...
if (shortcut[key] === false) {
// Don't call the callback and empty our temp tracking array
callme = false;
keysTmpArray.splice(0, keysTmpArray.length);
return;
}
// Otherwise, this key is being pressed.
// Add it to the array of keyboard keys we will send as an argument
// to our callback
keysTmpArray.push(key);
});
if (callme) {
shortcut.callback(keysTmpArray);
// Add this shortcut from our activate shortcuts array if not
// already activated
if (activatedShortcuts.indexOf(shortcut) === -1) {
activatedShortcuts.push(shortcut);
}
}
}
});
} else if (event.type === 'keyup') {
// Mark this key as currently not being pressed in all of our shortcuts
this.shortcuts.forEach((shortcut) => {
if (shortcut[event[this.eventProperty]] !== undefined) {
shortcut[event[this.eventProperty]] = false;
}
});
// Check if any of our currently pressed shortcuts have been released
// "released" means that all of the keys that the shortcut defines are no
// longer being pressed
this.activatedShortcuts.forEach((shortcut) => {
if (shortcut[event[this.eventProperty]] === undefined) return;
let shortcutReleased = true;
let keysTmpArray = [];
Object.keys(shortcut).forEach((key) => {
if (key === 'callback' || key === 'releaseCallback' || key === 'id')
return;
keysTmpArray.push(key);
// If any key is true, and thus still pressed, the shortcut is still
// being held
if (shortcut[key]) {
shortcutReleased = false;
}
});
if (shortcutReleased) {
// Call the released function handler
if (shortcut.releaseCallback) {
shortcut.releaseCallback(keysTmpArray);
}
// Remove this shortcut from our activate shortcuts array
const index = this.activatedShortcuts.indexOf(shortcut);
if (index !== -1) this.activatedShortcuts.splice(index, 1);
}
});
}
}
}
const iohook = new IOHook();
module.exports = iohook;