forked from wilix-team/iohook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
288 lines (250 loc) · 6.78 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
const EventEmitter = require('events');
const path = require('path');
// Try use handler if runtime and ABI is compatible
try {
const SegfaultHandler = require('segfault-handler');
SegfaultHandler.registerHandler("iohook-crash.log");
} catch (e) {}
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.lastKeydownShift = false;
this.lastKeydownAlt = false;
this.lastKeydownCtrl = false;
this.lastKeydownMeta = false;
this.load();
this.setDebug(false);
}
/**
* Start hook process
* @param 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 call when shortcut pressed
* @return {number} ShortcutId for unregister
*/
registerShortcut(keys, callback) {
let shortcut = {};
let shortcutId = Date.now() + Math.random();
keys.forEach(keyCode => {
shortcut[keyCode] = false;
})
shortcut.id = shortcutId;
shortcut.callback = callback;
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 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);
}
/**
* 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;
}
if (event.type === 'keydown') {
this.shortcuts.forEach(shortcut => {
if (shortcut[event.keycode] !== undefined) {
shortcut[event.keycode] = true;
let keysTmpArray = [];
let callme = true;
Object.keys(shortcut).forEach(key => {
if (key === 'callback' || key === 'id') return;
if (shortcut[key] === false) {
callme = false;
keysTmpArray.splice(0, keysTmpArray.length);
return;
}
keysTmpArray.push(key);
});
if (callme) {
shortcut.callback(keysTmpArray);
}
}
});
} else if (event.type === 'keyup') {
this.shortcuts.forEach(shortcut => {
if (shortcut[event.keycode] !== undefined) shortcut[event.keycode] = false;
});
}
}
}
const iohook = new IOHook();
module.exports = iohook;