0
votes

The goal of this simple script is to detect an idle interval when a specific program is in focus and then send a simple keystroke when that idle interval has passed. I'm running this script on 4 PC's and I'm getting unexpected results. Some PC's minimize the window when the script runs. Other PC's run it as expected. The script is identical on each PC.

I'm invoking this script by right clicking on the script (that is, not running a compiled exe version of it). Running it as administrator seems to achieve better results on some clients, on one it makes no difference and minimizes the window.

As stated, on some PC's the script works as intended. There are no error messages, it just causes my window to minimize. Nothing in that code, to my newb eyes, should cause the window to minimize.

#Persistent
    SetTimer, Timer_check,3000

    Timer_check:
        if WinActive("ahk_exe gta5.exe")
        {
            if (A_TimeIdlePhysical > 31301 && WinActive("ahk_exe gta5.exe")) {
                Gosub, keepActive
                ToolTip, We're currently idle TimeIdle is %A_TimeIdle% and TimeIdlePhysical is %A_TimeIdlePhysical%
                sleep 1000
                ToolTip
            }

            if (A_TimeIdle < 31301) {
                ToolTip
            }
        }
    return

keepActive: ; keep active sub.
    if WinActive("ahk_exe gta5.exe")
    {
        Send, {` down} ; Press the ` key to keep us active. It holds the key for 0.2 seconds.
        Sleep 200
        Send, {` up}
    }
return```
1

1 Answers

1
votes

You're trying to send the accent/backtick, which is default escape character in AHK (`). To fix this, send a different character or escape the escape character, like so:

Send, {`` down}
Sleep 200
Send, {`` up}

Without it being escaped, it just sends the down- and up-keys. The WinKey+down combination minimizes a non-maximized window and that may somehow be related to why you're seeing the game window occasionally minimized.

https://www.autohotkey.com/docs/commands/_EscapeChar.htm


Edit:
#Persistent
SetTimer, Timer_check, 3000

Timer_check:
If WinActive("ahk_exe gta5.exe") {
    If (A_TimeIdlePhysical > 31301 && WinActive("ahk_exe gta5.exe")) {
        Send , z
        ToolTip, We're currently idle TimeIdle is %A_TimeIdle% and TimeIdlePhysical is %A_TimeIdlePhysical%
        sleep 1000
        ToolTip
    }
    If (A_TimeIdle < 31301)
        ToolTip
}
Return