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

54 lines
1.4 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);
this.enable = function (opts) {
opts = opts || {};
this._keyOnDownUp = opts.keyOnDownUp || true;
2020-03-31 14:41:17 +08:00
window.addEventListener('keyup', keydownHandle, false);
2019-11-29 12:51:58 +08:00
};
this.disable = function () {
2020-03-31 14:41:17 +08:00
window.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;