rt-sim-training-client/src/jmapNew/keyboardController.js

52 lines
1.4 KiB
JavaScript

import Eventful from 'zrender/src/mixin/Eventful';
import {keyboardEvents} from './config/keyboardEvents';
class KeyboardController extends Eventful {
constructor(jmap) {
super();
this.$jmap = jmap;
this.$zr = jmap.getZr();
this.events = jmap.getEvents();
this.initData();
this.initHandler(this.$zr);
}
initHandler(zr) {
if (zr) {
var keydownHandle = this.keydown.bind(this);
this.enable = function (opts) {
opts = opts || {};
this._keyOnDownUp = opts.keyOnDownUp || true;
window.addEventListener('keydown', keydownHandle, false);
};
this.disable = function () {
window.removeEventListener('keydown', keydownHandle, false);
};
this.dispose = function() {
this.disable();
};
}
}
initData() {
}
keydown(e) {
if (this._keyOnDownUp && !e.repeat) {
const currentEvent = keyboardEvents[e.key.toUpperCase()];
let str = '';
if (currentEvent && currentEvent.altKey === e.altKey && currentEvent.ctrlKey === e.ctrlKey && currentEvent.shiftKey === e.shiftKey) {
str = currentEvent.event;
}
this.trigger(this.events.Keyboard, str);
}
}
}
export default KeyboardController;