]> git.proxmox.com Git - mirror_novnc.git/commitdiff
Track keys using keyIdentifier
authorPierre Ossman <ossman@cendio.se>
Thu, 6 Jul 2017 10:52:42 +0000 (12:52 +0200)
committerPierre Ossman <ossman@cendio.se>
Thu, 6 Jul 2017 11:00:00 +0000 (13:00 +0200)
This is necessary on older iOS where code isn't provided.

core/input/devices.js
tests/test.keyboard.js

index db2a1ae67109893a035d9f74e4c00cbeb2a7dd14..6409f74b391a7e9848c23d61c29746a5bc3aa141 100644 (file)
@@ -92,16 +92,34 @@ Keyboard.prototype = {
 
     _getKeyCode: function (e) {
         var code = KeyboardUtil.getKeycode(e);
-        if (code === 'Unidentified') {
-            // Unstable, but we don't have anything else to go on
-            // (don't use it for 'keypress' events thought since
-            // WebKit sets it to the same as charCode)
-            if (e.keyCode && (e.type !== 'keypress')) {
-                code = 'Platform' + e.keyCode;
+        if (code !== 'Unidentified') {
+            return code;
+        }
+
+        // Unstable, but we don't have anything else to go on
+        // (don't use it for 'keypress' events thought since
+        // WebKit sets it to the same as charCode)
+        if (e.keyCode && (e.type !== 'keypress')) {
+            return 'Platform' + e.keyCode;
+        }
+
+        // A precursor to the final DOM3 standard. Unfortunately it
+        // is not layout independent, so it is as bad as using keyCode
+        if (e.keyIdentifier) {
+            // Non-character key?
+            if (e.keyIdentifier.substr(0, 2) !== 'U+') {
+                return e.keyIdentifier;
             }
+
+            var codepoint = parseInt(e.keyIdentifier.substr(2), 16);
+            var char = String.fromCharCode(codepoint);
+            // Some implementations fail to uppercase the symbols
+            char = char.toUpperCase();
+
+            return 'Platform' + char.charCodeAt();
         }
 
-        return code;
+        return 'Unidentified';
     },
 
     _handleKeyDown: function (e) {
index 332d88c402cf942862ce20136a73b9fd638fa35f..a42168e0187b5d5210f4498b5983ce6b2bae8a9b 100644 (file)
@@ -159,6 +159,33 @@ describe('Key Event Handling', function() {
             kbd._handleKeyUp(keyevent('keyup', {code: 'KeyA', key: 'a'}));
             expect(callback).to.not.have.been.called;
         });
+
+        describe('Legacy Events', function() {
+            it('should track keys using keyCode if no code', function(done) {
+                var kbd = new Keyboard({
+                onKeyEvent: function(keysym, code, down) {
+                    expect(keysym).to.be.equal(0x61);
+                    expect(code).to.be.equal('Platform65');
+                    if (!down) {
+                        done();
+                    }
+                }});
+                kbd._handleKeyDown(keyevent('keydown', {keyCode: 65, key: 'a'}));
+                kbd._handleKeyUp(keyevent('keyup', {keyCode: 65, key: 'b'}));
+            });
+            it('should track keys using keyIdentifier if no code', function(done) {
+                var kbd = new Keyboard({
+                onKeyEvent: function(keysym, code, down) {
+                    expect(keysym).to.be.equal(0x61);
+                    expect(code).to.be.equal('Platform65');
+                    if (!down) {
+                        done();
+                    }
+                }});
+                kbd._handleKeyDown(keyevent('keydown', {keyIdentifier: 'U+0041', key: 'a'}));
+                kbd._handleKeyUp(keyevent('keyup', {keyIdentifier: 'U+0041', key: 'b'}));
+            });
+        });
     });
 
     describe('Shuffle modifiers on macOS', function() {