仅当特定窗口处于活动状态时才如何激活脚本?

时间:2019-08-01 13:13:37

标签: autohotkey

我在使用脚本时遇到了问题,该脚本应该执行以下操作: -如果特定的窗口变为活动状态 -SetCapslockState,打开 -如果Windows失去焦点 -SetCapslockState,关闭

我尝试过:

#If WinActive("ahk_class blahblah")

SetCapslockState, On

和/或

#If !WinActive("ahk_class Chrome_WidgetWin_1")

SetCapslockState, Off

但这不起作用。

我也尝试过:

WinWaitActive, (mytitleofwindow)
if ErrorLevel
{
SetCapslockState, On

return
}
else

它也不起作用,否则我不会在这里寻求帮助... hihihi

我希望有人能帮助我! :)

2 个答案:

答案 0 :(得分:2)

您也可以使用硬件句柄代替循环(占用CPU周期)。参见示例:

#SingleInstance Force
#installKeybdHook
#Persistent
Menu, Tray, Tip, Medical Alert
SetKeyDelay, 50
Menu, Tray, Icon , Shell32.dll, 145, 1
TrayTip, Medical Alert, Started, 1

Gui +LastFound 
hWnd := WinExist()
DllCall( "RegisterShellHookWindow", UInt,Hwnd )
MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
OnMessage( MsgNum, "ShellMessage" )
Return

ShellMessage( wParam ) ; Run on Window switch
{
    If (wParam = 4) ; If Window Changed
    {        
        WinGetActiveTitle, Title
        if instr(Title, "Past Medical History") OR instr(Title, "Allergies Verified") ; TESTED WITH: if instr(Title, "NotePad")
            MsgBox, 1, Allergies Verified, Please verify patient allergies
    }
}
Return

答案 1 :(得分:0)

关键是将WinWait[Not]ActiveLoop结合起来。

Loop {
    WinWaitActive, mytitleofwindow
    SetCapslockState, On
    WinWaitNotActive, mytitleofwindow
    SetCapslockState, Off
}