diff options
author | Devaev Maxim <[email protected]> | 2018-07-25 23:01:28 +0300 |
---|---|---|
committer | Devaev Maxim <[email protected]> | 2018-07-25 23:01:28 +0300 |
commit | 940fe28a9f717236a24f18930d14ff2804155480 (patch) | |
tree | a98117ae6fdbc990b4b987de0b17a18c2cd66d31 /kvmd/web/js/keyboard.js | |
parent | e2e8001ba8dec95742e6c121c7252b4fd5634b7d (diff) |
simplified hid
Diffstat (limited to 'kvmd/web/js/keyboard.js')
-rw-r--r-- | kvmd/web/js/keyboard.js | 36 |
1 files changed, 18 insertions, 18 deletions
diff --git a/kvmd/web/js/keyboard.js b/kvmd/web/js/keyboard.js index 798b9192..e2d02093 100644 --- a/kvmd/web/js/keyboard.js +++ b/kvmd/web/js/keyboard.js @@ -1,23 +1,22 @@ 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"; + var __ws = null; + + this.init = function() { + document.onkeydown = (event) => __keyHandler(event, true); + document.onkeyup = (event) => __keyHandler(event, false); }; - this.clearCapture = function() { - tools.info("Removing keyboard capture ...") - document.onkeydown = null; - document.onkeyup = null; - $("hid-keyboard-led").className = "led-off"; + this.setSocket = function(ws) { + __ws = ws; + $("hid-keyboard-led").className = (ws ? "led-on" : "led-off"); }; - var __keyHandler = function(ws, event, state) { + var __keyHandler = function(event, state) { // https://github.com/wesbos/keycodes/blob/gh-pages/scripts.js el_key = $(event.code); if (el_key) { + event.preventDefault(); + tools.debug("Key", (state ? "pressed:" : "released:"), event); if (state) { @@ -28,12 +27,13 @@ var keyboard = new function() { el_key.removeAttribute("style"); } - event.preventDefault(); - ws.send(JSON.stringify({ - event_type: "key", - key: event.code, - state: state, - })); + if (__ws) { + __ws.send(JSON.stringify({ + event_type: "key", + key: event.code, + state: state, + })); + } } }; }; |