Track keys using keyIdentifier

This is necessary on older iOS where code isn't provided.
This commit is contained in:
Pierre Ossman 2017-07-06 12:52:42 +02:00
parent 844e983916
commit 7e79dfe425
2 changed files with 53 additions and 8 deletions

View File

@ -92,16 +92,34 @@ Keyboard.prototype = {
_getKeyCode: function (e) { _getKeyCode: function (e) {
var code = KeyboardUtil.getKeycode(e); var code = KeyboardUtil.getKeycode(e);
if (code === 'Unidentified') { if (code !== 'Unidentified') {
return code;
}
// Unstable, but we don't have anything else to go on // Unstable, but we don't have anything else to go on
// (don't use it for 'keypress' events thought since // (don't use it for 'keypress' events thought since
// WebKit sets it to the same as charCode) // WebKit sets it to the same as charCode)
if (e.keyCode && (e.type !== 'keypress')) { if (e.keyCode && (e.type !== 'keypress')) {
code = 'Platform' + e.keyCode; return 'Platform' + e.keyCode;
}
} }
return code; // 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 'Unidentified';
}, },
_handleKeyDown: function (e) { _handleKeyDown: function (e) {

View File

@ -159,6 +159,33 @@ describe('Key Event Handling', function() {
kbd._handleKeyUp(keyevent('keyup', {code: 'KeyA', key: 'a'})); kbd._handleKeyUp(keyevent('keyup', {code: 'KeyA', key: 'a'}));
expect(callback).to.not.have.been.called; 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() { describe('Shuffle modifiers on macOS', function() {