-
Notifications
You must be signed in to change notification settings - Fork 35
/
term_keyboard.js
132 lines (121 loc) · 3.08 KB
/
term_keyboard.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
'use strict';
const KeyMap = {
'Backspace': '\b',
'Tab': '\t',
'Enter': '\r',
'Escape': '\x1b',
'Home': '\x1b[1~',
'Insert': '\x1b[2~',
'Delete': '\x1b[3~',
'End': '\x1b[4~',
'PageUp': '\x1b[5~',
'PageDown': '\x1b[6~',
'ArrowUp': '\x1b[A',
'ArrowDown': '\x1b[B',
'ArrowRight': '\x1b[C',
'ArrowLeft': '\x1b[D',
// Edge.
'Up': '\x1b[A',
'Down': '\x1b[B',
'Right': '\x1b[C',
'Left': '\x1b[D'
};
let CtrlShiftMap = {
'@': 50,
'^': 54,
'_': 109,
'?': 127,
'[': 219,
'\\': 220,
']': 221
};
// A -> 1
for (let i = 97; i <= 122; i++) {
CtrlShiftMap[String.fromCharCode(i)] = i - 96;
}
// FIXME: Under Mac, IME inputs will be sent as key of modified char.
// Need to use key code directly.
export class TermKeyboard {
// isLeftDB: function() -> bool
// isCurDB: function() -> bool
// send: function(data)
constructor(isLeftDB, isCurDB, send) {
this._checkLeftDB = isLeftDB;
this._checkCurDB = isCurDB;
this._sendFunc = send;
}
_send(data) {
this._sendFunc(data);
return true;
}
_sendCharCode(code) {
return this._send(String.fromCharCode(code));
}
_checkDB(key) {
switch (key) {
case 'Backspace':
case 'ArrowLeft':
return this._checkLeftDB();
case 'Delete':
case 'ArrowRight':
return this._checkCurDB();
}
return false;
}
onKeyDown(e) {
if (this._onKeyDown(e))
e.preventDefault();
}
_onKeyDown(e) {
// Windows/Command key.
if (e.getModifierState('Meta')) {
return false;
}
if (!e.ctrlKey && !e.altKey) {
// Shift-Insert as paste.
if (e.shiftKey && e.key == 'Insert') {
return false;
}
let mapped = KeyMap[e.key];
if (mapped) {
if (this._checkDB(e.key)) {
return this._send(mapped + mapped);
} else {
return this._send(mapped);
}
} else if (e.key.length == 1) {
// Normal char is handled in keypress. See comment in onKeyPress.
return false;
}
} else if (e.ctrlKey && !e.altKey && !e.shiftKey) {
// Use lowercase no even capslock's on.
let key = e.key.length == 1 ? e.key.toLowerCase() : e.key;
let mappedCode = CtrlShiftMap[key];
if (mappedCode) {
return this._sendCharCode(mappedCode);
}
} else if (!e.ctrlKey && e.altKey && !e.shiftKey) {
// Remapped keys, which conflict browser shortcuts.
// Use lowercase no even capslock's on.
switch (e.key.toLowerCase()) {
case 'r':
case 't':
case 'w':
// Ctrl+key
return this._sendCharCode(e.key.toUpperCase().charCodeAt(0) - 64);
}
}
return false;
}
onKeyPress(e) {
// Firefox on Mac issues keyCode for the key that starts composition (while
// other browsers send 229), so a normal char is handled using keypress. We
// can't move all key handling here since ctrl- and alt-compounds are
// handled by browsers before keypress.
if (!e.ctrlKey && !e.altKey && e.key.length == 1) {
e.preventDefault();
return this._send(e.key)
}
return false;
}
}