-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.js
47 lines (41 loc) · 1.03 KB
/
input.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
const EventEmitter = require("events");
const Keys = {
up: Buffer.from([0x1B, 0x5B, 0x41]),
down: Buffer.from([0x1B, 0x5B, 0x42]),
right: Buffer.from([0x1B, 0x5B, 0x43]),
left: Buffer.from([0x1B, 0x5B, 0x44]),
left: Buffer.from([0x1B, 0x5B, 0x44]),
tab: Buffer.from([0x07]),
enter: Buffer.from([0x0A]),
space: Buffer.from([0x20]),
};
function isPrint(char) {
if(char >= 'a' && char <= 'z') return true;
if(char >= 'A' && char <= 'Z') return true;
if(char >= '0' && char <= '9') return true;
if(char >= '[' && char <= '_') return true;
if(char >= '{' && char <= '~') return true;
return false;
}
class Input extends EventEmitter {
constructor(stream) {
super();
stream.on("data", this.handle.bind(this));
this.stream = stream;
}
handle(data) {
for(let i in Keys) {
if(data.equals(Keys[i])) this.emit(i);
}
const key = data.toString();
if(isPrint(key)) this.emit(key);
this.emit("data", data);
}
hijack() {
this.stream.setRawMode(true);
}
close() {
this.stream?.setRawMode(false);
}
}
module.exports = Input;