英文:
No 'OnMouseClick'-Trigger?
问题
我希望点击其他地方时,Autohotkey可以立即关闭一个窗口,我确信会有一个适合的触发器/监听器,比如OnMouseClick - 函数。但似乎没有;MouseClick 只是主动点击鼠标。所以我能想到的最好方法是这样的:
SetTitleMatchMode, 2
Loop{
WinGetActiveTitle, OutputVar
if (OutputVar <> "My_Window_Name_to_close.ahk"){
WinClose , AutoTasks 2.ahk
}
Sleep, 1000
}
return
尽管上面的代码确实有效,但我觉得这可能不是AHK能做的最好的方式(?),因为尽管这几行代码不是真正的问题,但它们会占用CPU和内存(根据我的任务管理器,比如Skype或我的云后台服务还多)。1秒的休眠也会导致可察觉的延迟,但如果没有它,循环甚至会占用2%的CPU!
您能想出一个更高效/本地化的AHK解决方案吗(除了进一步增加休眠时间)?
英文:
I wanted to have a window closed by Autohotkey as soon as I click somewhere else and was confident that there would be a fitting trigger / listener like e.g. an OnMouseClick - function. But seems not; MouseClick just actively clicks the mouse. So the best way I could figure was this:
SetTitleMatchMode, 2
Loop{
WinGetActiveTitle, OutputVar
if (OutputVar <> "My_Window_Name_to_close.ahk"){
WinClose , AutoTasks 2.ahk
}
Sleep, 1000
}
return
While the above code does work, it leaves me unsatisfied that that's really the best AHK can do (?), because those few lines of code, while not a real problem, do cost CPU and memory (according to my Task Manager e.g. more than Skype or my Cloud Background service). The 1-second-sleep also introduces a discernible lag, but without it, the loop would even cost 2% CPU!
Can you come up with a more efficient / AHK-native solution (except for further increasing the Sleep-time)?
答案1
得分: 2
即使你已经找到了解决方案,我想提供一个检测点击的实际问题的答案。可以使用与波浪符“~”修饰符结合使用“LButton”热键来完成:
~LButton:: ; “~”设置它,使键的原生功能不会被阻止
GoSub CheckActiveWindow
Return
CheckActiveWindow:
Sleep, 1 ; 等待一小段时间,以防所需的窗口从非活动状态切换到活动状态
WinGetActiveTitle, OutputVar
if (OutputVar <> "My_Window_Name_to_close.ahk"){
WinClose, AutoTasks 2.ahk
}
Return
英文:
Even though you found a solution to your problem, I wanted to provide an answer to the actual question of detecting a click. That can be done using a hotkey for "LButton" in combination with the tilde "~" modifier like so:
~LButton:: ; The "~" sets it so the key's native function will not be blocked
GoSub CheckActiveWindow
Return
CheckActiveWindow:
sleep, 1 ; Wait a small amount of time in case the desired Window is changing from inactive to Active
WinGetActiveTitle, OutputVar
if (OutputVar <> "My_Window_Name_to_close.ahk"){
WinClose , AutoTasks 2.ahk
}
return
答案2
得分: 1
SetTitleMatchMode, 3 ; 精确匹配
WinWaitNotActive, My_Window_Name_to_close.ahk
WinClose, My_Window_Name_to_close.ahk
英文:
I've found the answer myself in the meantime:
SetTitleMatchMode, 3 ; exact match
WinWaitNotActive, My_Window_Name_to_close.ahk
WinClose, My_Window_Name_to_close.ahk
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论