Search code examples
automationkeyboardautohotkeykeypress

How do I send output to an inactive window for better AFK? (AutoHotkey)


I'm trying to send the output of my code to an inactive application using Auto Hotkey on my computer so I don't have to be on the screen and can do other stuff. How would I go about implementing it?

F1::
stop := 0
Loop
{
    Send, z
    Sleep 500
}until Stop
return

F2::Stop := 1

This is the code I have down so far, any help?


Solution

  • ControlSending might work. It's basically a hit or miss. It'll work for some applications, and for some it wont.
    It'll be worth a try for you though.

    Also, you're going to want to use a timer as opposed to looping in a hotkey thread. Timer is intended just for something like this and what you were doing is kind of bad practice for various reasons.

    ;timer runs the user-defined function "SendZ" every 500ms
    F1::SetTimer, SendZ, 500 
    F2::SetTimer, SendZ, Off
    
    SendZ()
    {
        ControlSend, , z, % "ahk_exe notepad.exe"
    }
    

    As a bonus, we can even write a sweet one liner to toggle on/off that timer by using a ternary:
    F1::SetTimer, SendZ, % (Toggle:=!Toggle) ? 500 : "Off"
    If that doesn't make sense to you, and you're interested, you can read a lengthy previous explanation of mine about it here.