52 lines
1.4 KiB
JavaScript
52 lines
1.4 KiB
JavaScript
import Eventful from 'zrender/src/mixin/Eventful';
|
|
import { keyboardEvents } from './constant/keyboardEvents';
|
|
|
|
class KeyboardController extends Eventful {
|
|
constructor(iscs) {
|
|
super();
|
|
this.$iscs = iscs;
|
|
this.$zr = iscs.getZr();
|
|
this.events = iscs.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('keyup', keydownHandle, false);
|
|
};
|
|
|
|
this.disable = function () {
|
|
window.removeEventListener('keyup', 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;
|