Skip to content

Commit bc2cfe4

Browse files
feat(www): Allow to use physical keyboard as hid input
Import a pretty printed version of keysight.js from https://github.com/fresheneesz/keysight/blob/master/dist/keysight.umd.js and use it to catch the keyboard input in the web interface. Adjust the keyboard mappings accordingly allow virtual and physical keyboard input to be forwarded to the hid input at the same time. Signed-off-by: Tobias Schaffner <[email protected]>
1 parent bc3eb4f commit bc2cfe4

File tree

4 files changed

+243
-8
lines changed

4 files changed

+243
-8
lines changed

mtda/assets/keysight.js

+198
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
/* ---------------------------------------------------------------------------
2+
Copyright (c) 2013 by fresheneesz (https://github.com/fresheneesz/keysight)
3+
---------------------------------------------------------------------------
4+
SPDX-License-Identifier: MIT
5+
---------------------------------------------------------------------------
6+
*/
7+
8+
! function(e, n) {
9+
"object" == typeof exports && "object" == typeof module ? module.exports = n() : "function" == typeof define && define.amd ? define(n) : "object" == typeof exports ? exports.keysight = n() : e.keysight = n()
10+
}(this, function() {
11+
return function(e) {
12+
function n(t) {
13+
if (r[t]) return r[t].exports;
14+
var f = r[t] = {
15+
exports: {},
16+
id: t,
17+
loaded: !1
18+
};
19+
return e[t].call(f.exports, f, f.exports, n), f.loaded = !0, f.exports
20+
}
21+
var r = {};
22+
return n.m = e, n.c = r, n.p = "", n(0)
23+
}([function(e) {
24+
function n(e) {
25+
var n = String.fromCharCode(e);
26+
return n in t ? t[n] : n in s ? s[n] : n.toLowerCase()
27+
}
28+
var r = {
29+
"/": "?",
30+
".": ">",
31+
",": "<",
32+
"'": '"',
33+
";": ":",
34+
"[": "{",
35+
"]": "}",
36+
"\\": "|",
37+
"`": "~",
38+
"=": "+",
39+
"-": "_",
40+
1: "!",
41+
2: "@",
42+
3: "#",
43+
4: "$",
44+
5: "%",
45+
6: "^",
46+
7: "&",
47+
8: "*",
48+
9: "(",
49+
0: ")"
50+
},
51+
t = {};
52+
for (var f in r) {
53+
var o = r[f];
54+
t[o] = f
55+
}
56+
var i = {
57+
0: "\\",
58+
8: "\b",
59+
9: " ",
60+
12: "num",
61+
13: "\n",
62+
16: "shift",
63+
17: "meta",
64+
18: "alt",
65+
19: "pause",
66+
20: "caps",
67+
27: "esc",
68+
32: " ",
69+
33: "pageup",
70+
34: "pagedown",
71+
35: "end",
72+
36: "home",
73+
37: "left",
74+
38: "up",
75+
39: "right",
76+
40: "down",
77+
44: "print",
78+
45: "insert",
79+
46: "delete",
80+
91: "cmd",
81+
92: "cmd",
82+
93: "cmd",
83+
96: "num0",
84+
97: "num1",
85+
98: "num2",
86+
99: "num3",
87+
100: "num4",
88+
101: "num5",
89+
102: "num6",
90+
103: "num7",
91+
104: "num8",
92+
105: "num9",
93+
106: "*",
94+
107: "+",
95+
108: "num_enter",
96+
109: "num_subtract",
97+
110: "num_decimal",
98+
111: "num_divide",
99+
112: "f1",
100+
113: "f2",
101+
114: "f3",
102+
115: "f4",
103+
116: "f5",
104+
117: "f6",
105+
118: "f7",
106+
119: "f8",
107+
120: "f9",
108+
121: "f10",
109+
122: "f11",
110+
123: "f12",
111+
124: "print",
112+
144: "num",
113+
145: "scroll",
114+
173: "-",
115+
186: ";",
116+
187: "=",
117+
188: ",",
118+
189: "-",
119+
190: ".",
120+
191: "/",
121+
192: "`",
122+
219: "[",
123+
220: "\\",
124+
221: "]",
125+
222: "'",
126+
223: "`",
127+
224: "cmd",
128+
225: "alt",
129+
57392: "ctrl",
130+
63289: "num"
131+
},
132+
a = {};
133+
for (var f in i) {
134+
var u = i[f];
135+
a[u] = f
136+
}
137+
var s = {
138+
"\r": "\n"
139+
},
140+
d = {
141+
num_subtract: "-",
142+
num_enter: "\n",
143+
num_decimal: ".",
144+
num_divide: "/"
145+
};
146+
e.exports = function(e) {
147+
if ("keypress" === e.type) var t = n(e.charCode);
148+
else if (void 0 !== e.keyCode)
149+
if (e.keyCode in i) var t = i[e.keyCode];
150+
else var t = String.fromCharCode(e.keyCode).toLowerCase();
151+
else if (0 === e.charCode) var t = "\n";
152+
if (e.shiftKey && t in r) var f = r[t];
153+
else if (!e.shiftKey || t in a)
154+
if (t in d) var f = d[t];
155+
else var f = t;
156+
else var f = t.toUpperCase();
157+
return {
158+
"char": f,
159+
key: t
160+
}
161+
}, e.exports.unprintableKeys = {
162+
"\b": 1,
163+
num: 1,
164+
shift: 1,
165+
meta: 1,
166+
alt: 1,
167+
pause: 1,
168+
caps: 1,
169+
esc: 1,
170+
pageup: 1,
171+
pagedown: 1,
172+
end: 1,
173+
home: 1,
174+
left: 1,
175+
up: 1,
176+
right: 1,
177+
down: 1,
178+
print: 1,
179+
insert: 1,
180+
"delete": 1,
181+
cmd: 1,
182+
f1: 1,
183+
f2: 1,
184+
f3: 1,
185+
f4: 1,
186+
f5: 1,
187+
f6: 1,
188+
f7: 1,
189+
f8: 1,
190+
f9: 1,
191+
f10: 1,
192+
f11: 1,
193+
f12: 1,
194+
scroll: 1,
195+
ctrl: 1
196+
}
197+
}])
198+
});

mtda/keyboard/hid.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ def write(self, what):
220220
self.mtda.debug(3, "keyboard.hid.write()")
221221

222222
bsl = '\\'
223+
bsp = '\b'
223224
ret = '\n'
224225
lower_keys = {
225226
'a': 0x04, 'b': 0x05, 'c': 0x06, 'd': 0x07, 'e': 0x08, 'f': 0x09,
@@ -228,15 +229,19 @@ def write(self, what):
228229
's': 0x16, 't': 0x17, 'u': 0x18, 'v': 0x19, 'w': 0x1a, 'x': 0x1b,
229230
'y': 0x1c, 'z': 0x1d, '1': 0x1e, '2': 0x1f, '3': 0x20, '4': 0x21,
230231
'5': 0x22, '6': 0x23, '7': 0x24, '8': 0x25, '9': 0x26, '0': 0x27,
231-
ret: 0x28, ' ': 0x2c, '-': 0x2d, '=': 0x2e, '[': 0x2f, ']': 0x30,
232-
bsl: 0x31, ';': 0x33, "'": 0x34, '`': 0x35, ',': 0x36, '.': 0x37,
233-
'/': 0x38
232+
ret: 0x28, bsp: 0x2a, ' ': 0x2c, '-': 0x2d, '=': 0x2e, '[': 0x2f,
233+
']': 0x30, bsl: 0x31, ';': 0x33, "'": 0x34, '`': 0x35, ',': 0x36,
234+
'.': 0x37, '/': 0x38
234235
}
235236
shift_keys = {
236-
'!': 0x1e, '@': 0x1f, '#': 0x20, '$': 0x21, '%': 0x22, '^': 0x23,
237-
'&': 0x24, '*': 0x25, '(': 0x26, ')': 0x27, '_': 0x2d, '+': 0x2e,
238-
'{': 0x2f, '}': 0x30, '|': 0x31, ':': 0x33, '"': 0x34, '~': 0x35,
239-
'<': 0x36, '>': 0x37, '?': 0x38
237+
'A': 0x04, 'B': 0x05, 'C': 0x06, 'D': 0x07, 'E': 0x08, 'F': 0x09,
238+
'G': 0x0a, 'H': 0x0b, 'I': 0x0c, 'J': 0x0d, 'K': 0x0e, 'L': 0x0f,
239+
'M': 0x10, 'N': 0x11, 'O': 0x12, 'P': 0x13, 'Q': 0x14, 'R': 0x15,
240+
'S': 0x16, 'T': 0x17, 'U': 0x18, 'V': 0x19, 'W': 0x1a, 'X': 0x1b,
241+
'Y': 0x1c, 'Z': 0x1d, '!': 0x1e, '@': 0x1f, '#': 0x20, '$': 0x21,
242+
'%': 0x22, '^': 0x23, '&': 0x24, '*': 0x25, '(': 0x26, ')': 0x27,
243+
'_': 0x2d, '+': 0x2e, '{': 0x2f, '}': 0x30, '|': 0x31, ':': 0x33,
244+
'"': 0x34, '~': 0x35, '<': 0x36, '>': 0x37, '?': 0x38
240245
}
241246

242247
for k in what:

mtda/templates/index.html

+8
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,15 @@
106106
</script>
107107
<script src="./assets/jquery.min.js"></script>
108108
<script src="./assets/keyboard.js"></script>
109+
<script src="./assets/keysight.js"></script>
109110
<script type=text/javascript>
111+
window.addEventListener("keydown", function(event) {
112+
var key = keysight(event).char
113+
console.log("#### <KEY> " + key)
114+
$.getJSON('./keyboard-input', {input: key}, function(key) {
115+
// do nothing
116+
});
117+
});
110118
$(function() {
111119
$('a#power-toggle').bind('click', function() {
112120
document.getElementById("power-status-icon").innerHTML = "downloading"

mtda/www.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def console_input(data):
7676
def keyboard_input():
7777
mtda = app.config['mtda']
7878
map = {
79+
# virtual keyboard (keyboard.js)
7980
"Esc": mtda.keyboard.esc,
8081
"F1": mtda.keyboard.f1,
8182
"F2": mtda.keyboard.f2,
@@ -96,7 +97,30 @@ def keyboard_input():
9697
"Left": mtda.keyboard.left,
9798
"Right": mtda.keyboard.right,
9899
"Up": mtda.keyboard.up,
99-
"Down": mtda.keyboard.down
100+
"Down": mtda.keyboard.down,
101+
102+
# hid keyboard (keysight.js)
103+
"esc": mtda.keyboard.esc,
104+
"f1": mtda.keyboard.f1,
105+
"f2": mtda.keyboard.f2,
106+
"f3": mtda.keyboard.f3,
107+
"f4": mtda.keyboard.f4,
108+
"f5": mtda.keyboard.f5,
109+
"f6": mtda.keyboard.f6,
110+
"f7": mtda.keyboard.f7,
111+
"f8": mtda.keyboard.f8,
112+
"f9": mtda.keyboard.f9,
113+
"f10": mtda.keyboard.f10,
114+
"f11": mtda.keyboard.f11,
115+
"f12": mtda.keyboard.f12,
116+
"\b": mtda.keyboard.backspace,
117+
" ": mtda.keyboard.tab,
118+
"caps": mtda.keyboard.capsLock,
119+
"\n": mtda.keyboard.enter,
120+
"left": mtda.keyboard.left,
121+
"right": mtda.keyboard.right,
122+
"up": mtda.keyboard.up,
123+
"down": mtda.keyboard.down,
100124
}
101125
if mtda is not None and mtda.keyboard is not None:
102126
input = request.args.get('input', '', type=str)

0 commit comments

Comments
 (0)