// Keyboard event handler
//
-export default function Keyboard(target) {
- this._target = target || null;
-
- this._keyDownList = {}; // List of depressed keys
- // (even if they are happy)
- this._pendingKey = null; // Key waiting for keypress
- this._altGrArmed = false; // Windows AltGr detection
-
- // keep these here so we can refer to them later
- this._eventHandlers = {
- 'keyup': this._handleKeyUp.bind(this),
- 'keydown': this._handleKeyDown.bind(this),
- 'keypress': this._handleKeyPress.bind(this),
- 'blur': this._allKeysUp.bind(this),
- 'checkalt': this._checkAlt.bind(this),
- };
-}
+export default class Keyboard {
+ constructor(target) {
+ this._target = target || null;
+
+ this._keyDownList = {}; // List of depressed keys
+ // (even if they are happy)
+ this._pendingKey = null; // Key waiting for keypress
+ this._altGrArmed = false; // Windows AltGr detection
+
+ // keep these here so we can refer to them later
+ this._eventHandlers = {
+ 'keyup': this._handleKeyUp.bind(this),
+ 'keydown': this._handleKeyDown.bind(this),
+ 'keypress': this._handleKeyPress.bind(this),
+ 'blur': this._allKeysUp.bind(this),
+ 'checkalt': this._checkAlt.bind(this),
+ };
-Keyboard.prototype = {
- // ===== EVENT HANDLERS =====
+ // ===== EVENT HANDLERS =====
- onkeyevent: function () {}, // Handler for key press/release
+ this.onkeyevent = () => {}; // Handler for key press/release
+ }
// ===== PRIVATE METHODS =====
- _sendKeyEvent: function (keysym, code, down) {
+ _sendKeyEvent(keysym, code, down) {
if (down) {
this._keyDownList[code] = keysym;
} else {
Log.Debug("onkeyevent " + (down ? "down" : "up") +
", keysym: " + keysym, ", code: " + code);
this.onkeyevent(keysym, code, down);
- },
+ }
- _getKeyCode: function (e) {
+ _getKeyCode(e) {
const code = KeyboardUtil.getKeycode(e);
if (code !== 'Unidentified') {
return code;
}
return 'Unidentified';
- },
+ }
- _handleKeyDown: function (e) {
+ _handleKeyDown(e) {
const code = this._getKeyCode(e);
let keysym = KeyboardUtil.getKeysym(e);
}
this._sendKeyEvent(keysym, code, true);
- },
+ }
// Legacy event for browsers without code/key
- _handleKeyPress: function (e) {
+ _handleKeyPress(e) {
stopEvent(e);
// Are we expecting a keypress?
}
this._sendKeyEvent(keysym, code, true);
- },
- _handleKeyPressTimeout: function (e) {
+ }
+
+ _handleKeyPressTimeout(e) {
// Did someone manage to sort out the key already?
if (this._pendingKey === null) {
return;
}
this._sendKeyEvent(keysym, code, true);
- },
+ }
- _handleKeyUp: function (e) {
+ _handleKeyUp(e) {
stopEvent(e);
const code = this._getKeyCode(e);
}
this._sendKeyEvent(this._keyDownList[code], code, false);
- },
+ }
- _handleAltGrTimeout: function () {
+ _handleAltGrTimeout() {
this._altGrArmed = false;
clearTimeout(this._altGrTimeout);
this._sendKeyEvent(KeyTable.XK_Control_L, "ControlLeft", true);
- },
+ }
- _allKeysUp: function () {
+ _allKeysUp() {
Log.Debug(">> Keyboard.allKeysUp");
for (let code in this._keyDownList) {
this._sendKeyEvent(this._keyDownList[code], code, false);
}
Log.Debug("<< Keyboard.allKeysUp");
- },
+ }
// Firefox Alt workaround, see below
- _checkAlt: function (e) {
+ _checkAlt(e) {
if (e.altKey) {
return;
}
const target = this._target;
const downList = this._keyDownList;
- ['AltLeft', 'AltRight'].forEach(function (code) {
+ ['AltLeft', 'AltRight'].forEach((code) => {
if (!(code in downList)) {
return;
}
code: code });
target.dispatchEvent(event);
});
- },
+ }
// ===== PUBLIC METHODS =====
- grab: function () {
+ grab() {
//Log.Debug(">> Keyboard.grab");
this._target.addEventListener('keydown', this._eventHandlers.keydown);
const handler = this._eventHandlers.checkalt;
['mousedown', 'mouseup', 'mousemove', 'wheel',
'touchstart', 'touchend', 'touchmove',
- 'keydown', 'keyup'].forEach(function (type) {
+ 'keydown', 'keyup'].forEach(type =>
document.addEventListener(type, handler,
{ capture: true,
- passive: true });
- });
+ passive: true }));
}
//Log.Debug("<< Keyboard.grab");
- },
+ }
- ungrab: function () {
+ ungrab() {
//Log.Debug(">> Keyboard.ungrab");
if (browser.isWindows() && browser.isFirefox()) {
const handler = this._eventHandlers.checkalt;
['mousedown', 'mouseup', 'mousemove', 'wheel',
'touchstart', 'touchend', 'touchmove',
- 'keydown', 'keyup'].forEach(function (type) {
- document.removeEventListener(type, handler);
- });
+ 'keydown', 'keyup'].forEach(type => document.removeEventListener(type, handler));
}
this._target.removeEventListener('keydown', this._eventHandlers.keydown);
this._allKeysUp();
//Log.Debug(">> Keyboard.ungrab");
- },
-};
+ }
+}