mirror of
https://github.com/barsdeveloper/ueblueprint.git
synced 2026-02-03 23:55:04 +08:00
51 lines
1.3 KiB
JavaScript
Executable File
51 lines
1.3 KiB
JavaScript
Executable File
import Context from "./Context"
|
|
|
|
export default class KeyboardShortcut extends Context {
|
|
|
|
constructor(target, blueprint, options = {}) {
|
|
options.wantsFocusCallback = true
|
|
super(target, blueprint, options)
|
|
|
|
/** @type {String[]} */
|
|
this.key = this.options.key
|
|
this.ctrlKey = options.ctrlKey ?? false
|
|
this.shiftKey = options.shiftKey ?? false
|
|
this.altKey = options.altKey ?? false
|
|
this.metaKey = options.metaKey ?? false
|
|
|
|
let self = this
|
|
this.keyDownHandler = e => {
|
|
if (
|
|
e.code == self.key
|
|
&& e.ctrlKey === self.ctrlKey
|
|
&& e.shiftKey === self.shiftKey
|
|
&& e.altKey === self.altKey
|
|
&& e.metaKey === self.metaKey
|
|
) {
|
|
self.fire()
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param {String} keyString
|
|
* @returns {Object}
|
|
*/
|
|
static keyOptionsParse(options, keyString) {
|
|
options.key = keyString
|
|
return options
|
|
}
|
|
|
|
blueprintFocused() {
|
|
document.addEventListener("keydown", this.keyDownHandler)
|
|
}
|
|
|
|
blueprintUnfocused() {
|
|
document.removeEventListener("keydown", this.keyDownHandler)
|
|
}
|
|
|
|
fire() {
|
|
}
|
|
}
|