]> git.proxmox.com Git - mirror_novnc.git/blobdiff - core/input/devices.js
Fallback for missing keypress events
[mirror_novnc.git] / core / input / devices.js
index 6409f74b391a7e9848c23d61c29746a5bc3aa141..b530fc2ab23ec86a05caa5833b496db2d908cc6e 100644 (file)
@@ -187,6 +187,10 @@ Keyboard.prototype = {
         // just check for the presence of that field)
         if (!keysym && (!e.key || isIE() || isEdge())) {
             this._pendingKey = code;
+            // However we might not get a keypress event if the key
+            // is non-printable, which needs some special fallback
+            // handling
+            setTimeout(this._handleKeyPressTimeout.bind(this), 10, e);
             return;
         }
 
@@ -229,6 +233,43 @@ Keyboard.prototype = {
 
         this._sendKeyEvent(keysym, code, true);
     },
+    _handleKeyPressTimeout: function (e) {
+        if (!this._focused) { return; }
+
+        // Did someone manage to sort out the key already?
+        if (this._pendingKey === null) {
+            return;
+        }
+
+        var code, keysym;
+
+        code = this._pendingKey;
+        this._pendingKey = null;
+
+        // We have no way of knowing the proper keysym with the
+        // information given, but the following are true for most
+        // layouts
+        if ((e.keyCode >= 0x30) && (e.keyCode <= 0x39)) {
+            // Digit
+            keysym = e.keyCode;
+        } else if ((e.keyCode >= 0x41) && (e.keyCode <= 0x5a)) {
+            // Character (A-Z)
+            var char = String.fromCharCode(e.keyCode);
+            // A feeble attempt at the correct case
+            if (e.shiftKey)
+                char = char.toUpperCase();
+            else
+                char = char.toLowerCase();
+            keysym = char.charCodeAt();
+        } else {
+            // Unknown, give up
+            keysym = 0;
+        }
+
+        this._keyDownList[code] = keysym;
+
+        this._sendKeyEvent(keysym, code, true);
+    },
 
     _handleKeyUp: function (e) {
         if (!this._focused) { return; }