正如 Armin 已经写的,使用#Persistent。此外,如果您想创建仅在特定应用程序处于焦点时才处于活动状态的热键,您可以执行以下操作:在这种情况下,脚本将不再在启动时执行,只有当您按下热键时...
#Persistent
#SingleInstance
#IfWinActive, ahk_class TMainForm
!M::
sleep 2000
Send Now is the time
Return
!n::SoundBeep, 500, 500
!o::MsgBox, OK
#IfWinActive
这样所有 3 个(虚拟)热键将仅在您的应用程序处于焦点时才处于活动状态!您可以为另一个应用程序定义相同的热键,只需重复代码,但如果 #IfWinActive, ahk_class TMainForm 行中的另一个应用程序使用 ID。
如果您想在应用程序处于活动状态时每 2 秒发送一条消息,请执行以下操作:
#Persistent
#SingleInstance
SetTimerMatchMode, CheckApp, 2000
Return
CheckApp:
IfWinActive, ahk_class TMainForm
{
Send, Now is the time
}
Return
如果您想在每次(重新)激活(聚焦)您的应用程序时执行一个脚本(所以不是每两秒一次),请使用以下命令:
#Persistent
#installKeybdHook
#SingleInstance
Gui +LastFound
hWnd := WinExist()
DllCall( "RegisterShellHookWindow", UInt,Hwnd )
MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
OnMessage( MsgNum, "ShellMessage" )
Return
ShellMessage( wParam )
{
If (wParam = 4)
{
IfWinActive ahk_class TMainForm
{
Send, Now is the time
}
}
}
Return