Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gui/MacroEditGui.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
#Include InputGui.ahk

class MacroEditGui {
static Hotkeys := ["f5", "f6", "delete", "numpaddot"]

__new() {
WindowHotkeyManager.Register(this, MacroEditGui.Hotkeys, this.OnSoftKey.Bind(this))
this.ParentTile := ""
this.Gui := ""
this.GuiMenu := ""
Expand Down
12 changes: 12 additions & 0 deletions Main/BindUtil.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,16 @@ BindSoftHotKey() {
for index, value in MySoftData.SoftHotKeyArr {
mapKey := Trim(value, "~")
mapKey := StrLower(mapKey)

if (WindowHotkeyManager.IsManaged(mapKey)) {
key := "$*" mapKey
actionDown := OnBindKeyDown.Bind(value)
actionUp := OnBindKeyUp.Bind(value)
Hotkey(key, actionDown, "On")
Hotkey(key " up", actionUp, "On")
continue
}

isMenuBtnHotKey := CheckIfMenuBtnHotKey(mapKey)
isOpenMenu := MySoftData.CurMenuWheelIndex != -1
IsOnlySoftHotkey := MySoftData.TriggerKeyMap[mapKey].IsOnlySoftHotkey()
Expand Down Expand Up @@ -672,3 +682,5 @@ TriggerMacroHandler(tableIndex, itemIndex, *) {
OnTriggerMacroKeyAndInit(tableItem, macro, itemIndex)
}
}


19 changes: 16 additions & 3 deletions Main/TriggerKeyData.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ class TriggerKeyData {
this.HandleSoftHotKeyDown()
if (isMenuBtnHotKey && isOpenMenu)
return
if (WindowHotkeyManager.IsManaged(this.Key) && WindowHotkeyManager.IsAnyWindowActive(this.Key))
return
for index, value in this.DownArr {
if (index == 1 && SubStr(value.GetTK(), 1, 1) != "~")
LoosenModifyKey(value.GetTK())
Expand All @@ -117,6 +119,8 @@ class TriggerKeyData {
OnTriggerKeyUp() {
this.UpdataArr()
this.HandleSoftHotKeyUp()
if (WindowHotkeyManager.IsManaged(this.Key) && WindowHotkeyManager.IsAnyWindowActive(this.Key))
return
for index, value in this.LoosenArr {
value.Action()
}
Expand Down Expand Up @@ -195,9 +199,11 @@ class TriggerKeyData {
if (isMenuBtnHotKey)
MyMenuWheel.OnSoftKey(this.Key, true)

if (this.Key == "f5" || this.Key == "f6" || this.Key == "delete" || this.Key == "numpaddot") {
if (MySoftData.MacroEditGui != "")
MySoftData.MacroEditGui.OnSoftKey(this.Key, true)
if (WindowHotkeyManager.IsManaged(this.Key)) {
if (WindowHotkeyManager.HandleKey(this.Key, true))
return
SendLevel 1
Send("{Blind}{" this.Key "}")
}
}

Expand All @@ -218,6 +224,13 @@ class TriggerKeyData {
if (this.Key == "enter") {
MyColorPanel.OnEnterUp(this.Key)
}

if (WindowHotkeyManager.IsManaged(this.Key)) {
if (WindowHotkeyManager.HandleKey(this.Key, false))
return
SendLevel 1
Send("{Blind}{" this.Key " up}")
}
}
}

Expand Down
99 changes: 99 additions & 0 deletions Main/WindowHotkeyManager.ahk
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#Requires AutoHotkey v2.0

/**
* 窗口热键管理器
* 为特定窗口实例注册专属热键,窗口激活时拦截处理,失活时Send转发
*/
class WindowHotkeyManager {
static KeyHandlers := Map()

/**
* 注册窗口实例的热键
* @param instance 窗口实例(需有 Gui 属性或 GetWinHwnd 方法)
* @param hotkeys 热键数组,如 ["f5", "f6"]
* @param callback 回调函数 callback(key, isDown)
*/
static Register(instance, hotkeys, callback) {
for key in hotkeys {
key := StrLower(key)
if (!WindowHotkeyManager.KeyHandlers.Has(key))
WindowHotkeyManager.KeyHandlers[key] := []
WindowHotkeyManager.KeyHandlers[key].Push({
instance: instance,
callback: callback
})
}
}

/**
* 注销窗口实例的所有热键
*/
static Unregister(instance) {
for key, handlers in WindowHotkeyManager.KeyHandlers {
i := handlers.Length
while (i > 0) {
if (handlers[i].instance == instance)
handlers.RemoveAt(i)
i--
}
if (handlers.Length == 0)
WindowHotkeyManager.KeyHandlers.Delete(key)
}
}

/**
* 检查某个键是否被管理器管理
*/
static IsManaged(key) {
return WindowHotkeyManager.KeyHandlers.Has(StrLower(key))
}

/**
* 将按键分发给激活窗口的回调,返回是否被处理
*/
static HandleKey(key, isDown) {
key := StrLower(key)
handlers := WindowHotkeyManager.KeyHandlers[key]
if (!handlers)
return false
for h in handlers {
hwnd := WindowHotkeyManager.GetInstanceHwnd(h.instance)
if (hwnd && WinActive("ahk_id " hwnd)) {
h.callback.Call(key, isDown)
return true
}
}
return false
}

/**
* 检查某个键是否有激活的窗口
*/
static IsAnyWindowActive(key) {
key := StrLower(key)
handlers := WindowHotkeyManager.KeyHandlers[key]
if (!handlers)
return false
for h in handlers {
hwnd := WindowHotkeyManager.GetInstanceHwnd(h.instance)
if (hwnd && WinActive("ahk_id " hwnd))
return true
}
return false
}

/**
* 获取实例的窗口句柄
* 优先使用 Gui.Hwnd,其次调用 GetWinHwnd()
*/
static GetInstanceHwnd(instance) {
try {
if (IsObject(instance.Gui))
return instance.Gui.Hwnd
}
try {
return instance.GetWinHwnd()
}
return 0
}
}
1 change: 1 addition & 0 deletions RMT.ahk
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#Include Main\WorkPool.ahk
#Include Main\UIUtil.ahk
#Include Main\TimingUtil.ahk
#Include Main\WindowHotkeyManager.ahk
#Include Main\BindUtil.ahk
#Include Main\VariableUtil.ahk
#Include Main\TriggerKeyData.ahk
Expand Down