Remove keypress handling
We no longer support any browser that requires this legacy handling.
This commit is contained in:
parent
dccf6facdc
commit
1f7e1c7572
|
@ -20,14 +20,12 @@ export default class Keyboard {
|
||||||
|
|
||||||
this._keyDownList = {}; // List of depressed keys
|
this._keyDownList = {}; // List of depressed keys
|
||||||
// (even if they are happy)
|
// (even if they are happy)
|
||||||
this._pendingKey = null; // Key waiting for keypress
|
|
||||||
this._altGrArmed = false; // Windows AltGr detection
|
this._altGrArmed = false; // Windows AltGr detection
|
||||||
|
|
||||||
// keep these here so we can refer to them later
|
// keep these here so we can refer to them later
|
||||||
this._eventHandlers = {
|
this._eventHandlers = {
|
||||||
'keyup': this._handleKeyUp.bind(this),
|
'keyup': this._handleKeyUp.bind(this),
|
||||||
'keydown': this._handleKeyDown.bind(this),
|
'keydown': this._handleKeyDown.bind(this),
|
||||||
'keypress': this._handleKeyPress.bind(this),
|
|
||||||
'blur': this._allKeysUp.bind(this),
|
'blur': this._allKeysUp.bind(this),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -61,9 +59,7 @@ export default class Keyboard {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
if (e.keyCode) {
|
||||||
// WebKit sets it to the same as charCode)
|
|
||||||
if (e.keyCode && (e.type !== 'keypress')) {
|
|
||||||
// 229 is used for composition events
|
// 229 is used for composition events
|
||||||
if (e.keyCode !== 229) {
|
if (e.keyCode !== 229) {
|
||||||
return 'Platform' + e.keyCode;
|
return 'Platform' + e.keyCode;
|
||||||
|
@ -168,18 +164,6 @@ export default class Keyboard {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If this is a legacy browser then we'll need to wait for
|
|
||||||
// a keypress event as well
|
|
||||||
if (!keysym && !e.key) {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
this._pendingKey = null;
|
|
||||||
stopEvent(e);
|
stopEvent(e);
|
||||||
|
|
||||||
// Possible start of AltGr sequence? (see above)
|
// Possible start of AltGr sequence? (see above)
|
||||||
|
@ -194,69 +178,6 @@ export default class Keyboard {
|
||||||
this._sendKeyEvent(keysym, code, true);
|
this._sendKeyEvent(keysym, code, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Legacy event for browsers without code/key
|
|
||||||
_handleKeyPress(e) {
|
|
||||||
stopEvent(e);
|
|
||||||
|
|
||||||
// Are we expecting a keypress?
|
|
||||||
if (this._pendingKey === null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let code = this._getKeyCode(e);
|
|
||||||
const keysym = KeyboardUtil.getKeysym(e);
|
|
||||||
|
|
||||||
// The key we were waiting for?
|
|
||||||
if ((code !== 'Unidentified') && (code != this._pendingKey)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
code = this._pendingKey;
|
|
||||||
this._pendingKey = null;
|
|
||||||
|
|
||||||
if (!keysym) {
|
|
||||||
Log.Info('keypress with no keysym:', e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this._sendKeyEvent(keysym, code, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
_handleKeyPressTimeout(e) {
|
|
||||||
// Did someone manage to sort out the key already?
|
|
||||||
if (this._pendingKey === null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
let keysym;
|
|
||||||
|
|
||||||
const 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)
|
|
||||||
let 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._sendKeyEvent(keysym, code, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
_handleKeyUp(e) {
|
_handleKeyUp(e) {
|
||||||
stopEvent(e);
|
stopEvent(e);
|
||||||
|
|
||||||
|
@ -316,7 +237,6 @@ export default class Keyboard {
|
||||||
|
|
||||||
this._target.addEventListener('keydown', this._eventHandlers.keydown);
|
this._target.addEventListener('keydown', this._eventHandlers.keydown);
|
||||||
this._target.addEventListener('keyup', this._eventHandlers.keyup);
|
this._target.addEventListener('keyup', this._eventHandlers.keyup);
|
||||||
this._target.addEventListener('keypress', this._eventHandlers.keypress);
|
|
||||||
|
|
||||||
// Release (key up) if window loses focus
|
// Release (key up) if window loses focus
|
||||||
window.addEventListener('blur', this._eventHandlers.blur);
|
window.addEventListener('blur', this._eventHandlers.blur);
|
||||||
|
@ -329,7 +249,6 @@ export default class Keyboard {
|
||||||
|
|
||||||
this._target.removeEventListener('keydown', this._eventHandlers.keydown);
|
this._target.removeEventListener('keydown', this._eventHandlers.keydown);
|
||||||
this._target.removeEventListener('keyup', this._eventHandlers.keyup);
|
this._target.removeEventListener('keyup', this._eventHandlers.keyup);
|
||||||
this._target.removeEventListener('keypress', this._eventHandlers.keypress);
|
|
||||||
window.removeEventListener('blur', this._eventHandlers.blur);
|
window.removeEventListener('blur', this._eventHandlers.blur);
|
||||||
|
|
||||||
// Release (key up) all keys that are in a down state
|
// Release (key up) all keys that are in a down state
|
||||||
|
|
|
@ -22,9 +22,8 @@ export function getKeycode(evt) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// The de-facto standard is to use Windows Virtual-Key codes
|
// The de-facto standard is to use Windows Virtual-Key codes
|
||||||
// in the 'keyCode' field for non-printable characters. However
|
// in the 'keyCode' field for non-printable characters
|
||||||
// Webkit sets it to the same as charCode in 'keypress' events.
|
if (evt.keyCode in vkeys) {
|
||||||
if ((evt.type !== 'keypress') && (evt.keyCode in vkeys)) {
|
|
||||||
let code = vkeys[evt.keyCode];
|
let code = vkeys[evt.keyCode];
|
||||||
|
|
||||||
// macOS has messed up this code for some reason
|
// macOS has messed up this code for some reason
|
||||||
|
|
|
@ -42,110 +42,6 @@ describe('Key Event Handling', function () {
|
||||||
kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', key: 'a'}));
|
kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', key: 'a'}));
|
||||||
kbd._handleKeyUp(keyevent('keyup', {code: 'KeyA', key: 'a'}));
|
kbd._handleKeyUp(keyevent('keyup', {code: 'KeyA', key: 'a'}));
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Legacy keypress Events', function () {
|
|
||||||
it('should wait for keypress when needed', function () {
|
|
||||||
const kbd = new Keyboard(document);
|
|
||||||
kbd.onkeyevent = sinon.spy();
|
|
||||||
kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', keyCode: 0x41}));
|
|
||||||
expect(kbd.onkeyevent).to.not.have.been.called;
|
|
||||||
});
|
|
||||||
it('should decode keypress events', function (done) {
|
|
||||||
const kbd = new Keyboard(document);
|
|
||||||
kbd.onkeyevent = (keysym, code, down) => {
|
|
||||||
expect(keysym).to.be.equal(0x61);
|
|
||||||
expect(code).to.be.equal('KeyA');
|
|
||||||
expect(down).to.be.equal(true);
|
|
||||||
done();
|
|
||||||
};
|
|
||||||
kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', keyCode: 0x41}));
|
|
||||||
kbd._handleKeyPress(keyevent('keypress', {code: 'KeyA', charCode: 0x61}));
|
|
||||||
});
|
|
||||||
it('should ignore keypress with different code', function () {
|
|
||||||
const kbd = new Keyboard(document);
|
|
||||||
kbd.onkeyevent = sinon.spy();
|
|
||||||
kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', keyCode: 0x41}));
|
|
||||||
kbd._handleKeyPress(keyevent('keypress', {code: 'KeyB', charCode: 0x61}));
|
|
||||||
expect(kbd.onkeyevent).to.not.have.been.called;
|
|
||||||
});
|
|
||||||
it('should handle keypress with missing code', function (done) {
|
|
||||||
const kbd = new Keyboard(document);
|
|
||||||
kbd.onkeyevent = (keysym, code, down) => {
|
|
||||||
expect(keysym).to.be.equal(0x61);
|
|
||||||
expect(code).to.be.equal('KeyA');
|
|
||||||
expect(down).to.be.equal(true);
|
|
||||||
done();
|
|
||||||
};
|
|
||||||
kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', keyCode: 0x41}));
|
|
||||||
kbd._handleKeyPress(keyevent('keypress', {charCode: 0x61}));
|
|
||||||
});
|
|
||||||
it('should guess key if no keypress and numeric key', function (done) {
|
|
||||||
const kbd = new Keyboard(document);
|
|
||||||
kbd.onkeyevent = (keysym, code, down) => {
|
|
||||||
expect(keysym).to.be.equal(0x32);
|
|
||||||
expect(code).to.be.equal('Digit2');
|
|
||||||
expect(down).to.be.equal(true);
|
|
||||||
done();
|
|
||||||
};
|
|
||||||
kbd._handleKeyDown(keyevent('keydown', {code: 'Digit2', keyCode: 0x32}));
|
|
||||||
});
|
|
||||||
it('should guess key if no keypress and alpha key', function (done) {
|
|
||||||
const kbd = new Keyboard(document);
|
|
||||||
kbd.onkeyevent = (keysym, code, down) => {
|
|
||||||
expect(keysym).to.be.equal(0x61);
|
|
||||||
expect(code).to.be.equal('KeyA');
|
|
||||||
expect(down).to.be.equal(true);
|
|
||||||
done();
|
|
||||||
};
|
|
||||||
kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', keyCode: 0x41, shiftKey: false}));
|
|
||||||
});
|
|
||||||
it('should guess key if no keypress and alpha key (with shift)', function (done) {
|
|
||||||
const kbd = new Keyboard(document);
|
|
||||||
kbd.onkeyevent = (keysym, code, down) => {
|
|
||||||
expect(keysym).to.be.equal(0x41);
|
|
||||||
expect(code).to.be.equal('KeyA');
|
|
||||||
expect(down).to.be.equal(true);
|
|
||||||
done();
|
|
||||||
};
|
|
||||||
kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', keyCode: 0x41, shiftKey: true}));
|
|
||||||
});
|
|
||||||
it('should not guess key if no keypress and unknown key', function (done) {
|
|
||||||
const kbd = new Keyboard(document);
|
|
||||||
kbd.onkeyevent = (keysym, code, down) => {
|
|
||||||
expect(keysym).to.be.equal(0);
|
|
||||||
expect(code).to.be.equal('KeyA');
|
|
||||||
expect(down).to.be.equal(true);
|
|
||||||
done();
|
|
||||||
};
|
|
||||||
kbd._handleKeyDown(keyevent('keydown', {code: 'KeyA', keyCode: 0x09}));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('suppress the right events at the right time', function () {
|
|
||||||
it('should suppress anything with a valid key', function () {
|
|
||||||
const kbd = new Keyboard(document, {});
|
|
||||||
const evt1 = keyevent('keydown', {code: 'KeyA', key: 'a'});
|
|
||||||
kbd._handleKeyDown(evt1);
|
|
||||||
expect(evt1.preventDefault).to.have.been.called;
|
|
||||||
const evt2 = keyevent('keyup', {code: 'KeyA', key: 'a'});
|
|
||||||
kbd._handleKeyUp(evt2);
|
|
||||||
expect(evt2.preventDefault).to.have.been.called;
|
|
||||||
});
|
|
||||||
it('should not suppress keys without key', function () {
|
|
||||||
const kbd = new Keyboard(document, {});
|
|
||||||
const evt = keyevent('keydown', {code: 'KeyA', keyCode: 0x41});
|
|
||||||
kbd._handleKeyDown(evt);
|
|
||||||
expect(evt.preventDefault).to.not.have.been.called;
|
|
||||||
});
|
|
||||||
it('should suppress the following keypress event', function () {
|
|
||||||
const kbd = new Keyboard(document, {});
|
|
||||||
const evt1 = keyevent('keydown', {code: 'KeyA', keyCode: 0x41});
|
|
||||||
kbd._handleKeyDown(evt1);
|
|
||||||
const evt2 = keyevent('keypress', {code: 'KeyA', charCode: 0x41});
|
|
||||||
kbd._handleKeyPress(evt2);
|
|
||||||
expect(evt2.preventDefault).to.have.been.called;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Fake keyup', function () {
|
describe('Fake keyup', function () {
|
||||||
|
|
Loading…
Reference in New Issue