@avalon-dota-libs/ui_hotkeys
v0.0.7
Published
UI 中的按键注册
Downloads
11
Readme
@avalon-dota-libs/ui_hotkeys
UI 中的按键注册
HotKey
用于注册按键,HotKey
类包装了按键的注册和指令绑定,HotKey
继承了 @avalon-dota-libs/ui_utils
的
EventEmitter
,可通过EventEmitter
监听事件。
推荐使用 HotKeyCommand
import { HotKey } from '@avalon-dota-libs/ui_hotkeys';
const Q = HotKey.GetOrCreate('Q');
// 按下
Q.on('down', $('#MyButton'), function (key) {
if (key.Command?.Name === 'OpenUI') {
// ...
}
});
// 释放
Q.on('up', $('#MyButton'), function (key) {
// ...
});
HotKeyCommand
用于注册指令,HotKeyCommand
也继承了 EventEmitter
,HotKeyCommand 会自动绑定按键,可轻松解绑或切换绑定的按键,并且也支持 Ctrl、Alt、Shift 的触发限制。
import { HotKey } from '@avalon-dota-libs/ui_hotkeys';
const OpenUI = HotKeyCommand.GetOrCreate('OpenUI');
// 绑定按键
OpenUI.Bind('Q');
// 修改绑定的按键
OpenUI.Bind('W');
// 触发按键
OpenUI.on('down', $('#MyButton'), function (cmd) {
if (cmd.Key?.Key === 'Q') {
// ...
}
});
// 如果指令需要配合 Ctrl、Alt、Shift 等,可设置 Ctrl、Alt、Shift 的状态
OpenUI.Bind('Q').SetCtrl(true).SetAlt(true);
OpenUI.on('down', $('#MyButton'), function (cmd) {
// 此时需要按下 Ctrl、Alt 才能触发
});