summaryrefslogtreecommitdiff
path: root/kvmd/web/js/keyboard.js
diff options
context:
space:
mode:
authorDevaev Maxim <[email protected]>2018-07-15 11:21:44 +0300
committerDevaev Maxim <[email protected]>2018-07-15 11:21:44 +0300
commit999d3f245710013425cc2413f81d900ec1fc2f24 (patch)
tree07bf519cab5da1b522e8146a362724762c8a3e7c /kvmd/web/js/keyboard.js
parent4122ecdb55abae00f2168d27df5f88527fc02341 (diff)
big js refactoring
Diffstat (limited to 'kvmd/web/js/keyboard.js')
-rw-r--r--kvmd/web/js/keyboard.js27
1 files changed, 27 insertions, 0 deletions
diff --git a/kvmd/web/js/keyboard.js b/kvmd/web/js/keyboard.js
new file mode 100644
index 00000000..4f1ade8b
--- /dev/null
+++ b/kvmd/web/js/keyboard.js
@@ -0,0 +1,27 @@
+var keyboard = new function() {
+ this.installCapture = function(ws) {
+ // https://www.codeday.top/2017/05/03/24906.html
+ tools.info("Installing keyboard capture ...")
+ document.onkeydown = (event) => __keyHandler(ws, event, true);
+ document.onkeyup = (event) => __keyHandler(ws, event, false);
+ $("hid-keyboard-led").className = "led-on";
+ };
+
+ this.clearCapture = function() {
+ tools.info("Removing keyboard capture ...")
+ document.onkeydown = null;
+ document.onkeyup = null;
+ $("hid-keyboard-led").className = "led-off";
+ };
+
+ var __keyHandler = function(ws, event, state) {
+ // https://github.com/wesbos/keycodes/blob/gh-pages/scripts.js
+ tools.debug("Key", (state ? "pressed:" : "released:"), event)
+ event.preventDefault();
+ ws.send(JSON.stringify({
+ event_type: "key",
+ key: event.code,
+ state: state,
+ }));
+ };
+};