-
Notifications
You must be signed in to change notification settings - Fork 0
/
token_reader.js
43 lines (35 loc) · 910 Bytes
/
token_reader.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
const InputEvent = require('input-event');
class TokenReader {
// See description at https://www.kernel.org/doc/Documentation/input/input.txt
// Converted with regex from https://github.com/torvalds/linux/blob/master/include/uapi/linux/input-event-codes.h
keyCodeMappings = new Map([
[2, '1'],
[3, '2'],
[4, '3'],
[5, '4'],
[6, '5'],
[7, '6'],
// [8, '7'],
// [9, '8'],
[10, '9'],
[11, '0'],
[28, '\n'],
]);
constructor(devicename) {
const input = new InputEvent(devicename);
this.keyboard = new InputEvent.Keyboard(input);
}
onTokenEvent(callback) {
var buffer = '';
this.keyboard.on('keypress', obj => {
const char = keyCodeMappings.get(obj.code) || '?';
if (char === '\n') {
callback(buffer);
buffer = '';
} else {
buffer += char;
}
});
}
}
module.exports = TokenReader