没有’OnMouseClick’触发器?

huangapple go评论39阅读模式
英文:

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 &quot;~&quot; sets it so the key&#39;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 &lt;&gt; &quot;My_Window_Name_to_close.ahk&quot;){
        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

huangapple
  • 本文由 发表于 2023年2月14日 02:02:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75439634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定