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

56 lines
1.5 KiB
JavaScript
Raw Normal View History

2019-11-29 12:51:58 +08:00
import Eventful from 'zrender/src/mixin/Eventful';
import {keyboardEvents} from './config/keyboardEvents';
2019-11-29 12:51:58 +08:00
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);
2020-09-30 09:54:26 +08:00
var dom = this.$zr.dom;
2019-11-29 12:51:58 +08:00
this.enable = function (opts) {
opts = opts || {};
this._keyOnDownUp = opts.keyOnDownUp || true;
2020-09-30 14:56:37 +08:00
dom.addEventListener('keyup', keydownHandle, false);
dom.focus();
2019-11-29 12:51:58 +08:00
};
this.disable = function () {
2020-09-30 09:54:26 +08:00
dom.removeEventListener('keyup', keydownHandle, false);
2019-11-29 12:51:58 +08:00
};
this.dispose = function() {
this.disable();
};
}
}
initData() {
2019-11-29 12:51:58 +08:00
}
keydown(e) {
if (this._keyOnDownUp && !e.repeat) {
2020-06-23 18:59:20 +08:00
if (e.key) {
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);
2019-11-29 12:51:58 +08:00
}
}
}
}
export default KeyboardController;