Files
FiDA_Front/src/components/Canvas/DepthCanvas/manager/events/KeyEventManager.ts

39 lines
1.2 KiB
TypeScript
Raw Normal View History

2026-03-12 11:40:48 +08:00
export class KeyEventManager {
stateManager: any
constructor(options) {
this.stateManager = options.stateManager;
2026-03-17 09:56:58 +08:00
this._handleKeyDown = this.handleKeyDown.bind(this)
2026-03-12 11:40:48 +08:00
}
2026-03-16 16:51:12 +08:00
onMounted() { }
2026-03-12 11:40:48 +08:00
/** 处理键盘事件 */
2026-03-17 09:56:58 +08:00
_handleKeyDown: any
2026-03-12 11:40:48 +08:00
handleKeyDown(event: any) {
2026-03-17 17:17:48 +08:00
const activeID = this.stateManager.layerManager.activeID.value
2026-03-12 11:40:48 +08:00
const ctrl = event.ctrlKey ? 'ctrl-' : "";
const shift = event.shiftKey ? 'shift-' : "";
const key = event.key;
const reg = new RegExp(`^${ctrl}${shift}${key}$`, 'i')
const list = [
2026-03-17 17:17:48 +08:00
{ key: "ctrl-c", handler: () => this.stateManager.layerManager.copyLayerById(activeID) },
{ key: "delete", handler: () => this.stateManager.layerManager.deleteLayerById(activeID) },
2026-03-12 11:40:48 +08:00
{ key: "ctrl-z", handler: () => this.stateManager.undoState() },
{ key: "ctrl-shift-z", handler: () => this.stateManager.redoState() },
]
list.forEach((v: any) => {
if (reg.test(v.key)) v.handler(event)
})
}
/** 注册事件 */
registerEvents() {
2026-03-17 09:56:58 +08:00
document.addEventListener('keydown', this._handleKeyDown)
2026-03-12 11:40:48 +08:00
}
/** 删除事件 */
removeEvents() {
2026-03-17 09:56:58 +08:00
document.removeEventListener('keydown', this._handleKeyDown)
2026-03-12 11:40:48 +08:00
}
dispose() {
this.removeEvents()
}
}