X 秒不活动后自动关闭程序(在该程序中)

X 秒不活动后自动关闭程序(在该程序中)

使用 AutoHotkey,我想在 X 秒不活动后自动关闭程序(例如 Firefox)(在该程序中 - 即鼠标或键盘活动可能在其他地方,但在该程序中没有)。

我应该使用什么代码?谢谢

答案1

在 Firefox 处于非活动状态 20 秒后将其关闭:

#Persistent

SetTimer, close_inactive_Firefox_window, 1000 ; 1 second timer
return

    close_inactive_Firefox_window:
If !WinActive("ahk_exe firefox.exe")
    time++ ; checks the number in the variable "time" and increases it by 1 every 1 second (in accordance with the timer period)
else
    time := 0     ; reset
If (time = 20)    ; 20 seconds
{
    WinClose, ahk_exe firefox.exe
    time := 0     ; reset
}
return

https://www.autohotkey.com/docs/commands/SetTimer.htm https://www.autohotkey.com/docs/Variables.htm#operators

编辑

如果您想滚动非活动的 Firefox 窗口而不关闭它,请将其添加到上面的代码中:

#If MouseIsOver("ahk_exe firefox.exe")

    ~WheelUp::
    ~WheelDown::
        time := 0 ; reset
    return

#If

MouseIsOver(WinTitle){
    MouseGetPos,,, Win
    return WinExist(WinTitle . " ahk_id " . Win)
}

答案2

在这样的限制下进行创作实在是太复杂了。

如果计算机只是在 15 分钟无活动后关闭 Firefox,那么没问题。但是,没有内置的“程序内无活动”测量方法,因此您必须自己完成这一部分。然后,您必须处理每个按键、鼠标移动、鼠标点击,并将其归类为 Firefox 活动或非活动,然后在 15 分钟无活动后将其关闭。

相关内容