_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) {
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() {