AutoHotKey:如何查明某个 window 是否已与之交互

AutoHotKey: How to find out if a certain window has or hasn't been interacted with

我正在制作一个 AutoHotKey 脚本,如果满足某些条件,它会关闭 window。

起初,我的任务是将其设置为关闭 window 如果计算机在 5 分钟内未与其进行交互。 AutoHotKey 提供了一个变量,用于检查用户是否与计算机进行过任何交互,该变量是 A_TimeIdle。每次用户与计算机交互时它都会更新为 0,然后随着用户不与计算机交互而明显增加。

我现在需要的是 A_TimeIdle 之类的东西,但用于与特定 window 交互的用户而不是整个计算机。 autohotkey 是否提供类似的东西?有什么方法可以检查 window 闲置了多长时间,诸如此类?

使用 IfWinExist 命令。

可能你不知道 window 是否出现,所以我建议你插入一个循环来多次尝试。

i:=0
while(i < 5){
   IfWinExist, "Title"
   i := i + 1
   Sleep, 200 ;Wait a bit for the next comparison
}

另一个有用的命令是 WinWait。您可以在此处查看定义:https://autohotkey.com/docs/commands/WinWait.htm

IfWinExist: https://autohotkey.com/docs/commands/WinExist.htm

像这样:

#Persistent
SetTimer, check_for_window
return

    check_for_window:
IfWinNotExist, ahk_class Notepad
{
    Tooltip
    return
}
IfWinActive, ahk_class Notepad
{
    SetTimer, check_for_window, off
    Tooltip
    WinWaitNotActive, ahk_class Notepad
    SetTimer, time_window_has_been_inactive
}
return

    time_window_has_been_inactive:
Loop 60
{
    Sleep 100
    IfWinActive, ahk_class Notepad
        return
}
SetTimer, time_window_has_been_inactive, off
IfWinExist, ahk_class Notepad
{
    Tooltip, Notepad has been inactive for 6 seconds
    ; do sth
}
SetTimer, check_for_window, on
return

另请参阅: Minimize Inactive Windows