问题描述
我需要代码来在打开记事本时启用Hotstring并在关闭记事本时禁用Hotstring。因此,我在下面编写了代码,发现该代码有时会错过记事本打开的事件。我该如何解决?
while True
{
begin:
Sleep,1
;Untitled - Notepad
while not WinActive("Untitled - Notepad")
{
Sleep,1
if WinActive("Untitled - Notepad")
{
MsgBox WinActive
HotString("::hello","Hello World",On)
goto,begin
}
HotString("::hello",Off)
}
}
a::
ExitApp
解决方法
#Directive是用于此目的的,更具体地说是#IfWinActive
directive。
这是您的成品:
#IfWinActive,ahk_class Notepad
::hello::Hello World
#IfWinActive
为方便起见,使用ahk_class
。您可以详细了解here。
另外,也可以使用#IfWinActive,ahk_exe notepad.exe
。
或者,当然,普通的#IfWinActive,Untitled - Notepad
也可以使用,但是仅当您编辑名为“无标题”的文档时才可以。这就是为什么它不是一个好方法的原因。
AHKv1:
#If WinActive("ahk_class Notepad")
::hello::Hello World
#If
AHKv2:
#HotIf WinActive("ahk_class Notepad")
::hello::Hello World
#HotIf