diff options
author | Pierre Ossman <ossman@cendio.se> | 2017-07-06 12:52:42 +0200 |
---|---|---|
committer | Pierre Ossman <ossman@cendio.se> | 2017-07-06 13:00:00 +0200 |
commit | 7e79dfe425c0245b37f70eb694fa5a4bc687892b (patch) | |
tree | 617b30bdb16b6865dae4d5d2d64ba5ff69008e75 | |
parent | 844e98391689febb03ae5ea905eba15f12ed9d9e (diff) | |
download | novnc-7e79dfe425c0245b37f70eb694fa5a4bc687892b.tar.gz |
Track keys using keyIdentifier
This is necessary on older iOS where code isn't provided.
-rw-r--r-- | core/input/devices.js | 32 | ||||
-rw-r--r-- | tests/test.keyboard.js | 27 |
2 files changed, 52 insertions, 7 deletions
diff --git a/core/input/devices.js b/core/input/devices.js index db2a1ae..6409f74 100644 --- a/core/input/devices.js +++ b/core/input/devices.js @@ -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) { diff --git a/tests/test.keyboard.js b/tests/test.keyboard.js index 332d88c..a42168e 100644 --- a/tests/test.keyboard.js +++ b/tests/test.keyboard.js @@ -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() { |