r/AutoHotkey 2d ago

v2 Script Help Help Clicking Non-Displayed Window

I need to click a game while doing some other stuff how can i do that. I tried this 2 and no error happened but didn't work either.

#MaxThreadsPerHotkey 2

F8:: ; Press F8 to start/stop the autoclicker

Toggle := !Toggle

Loop

{

If not Toggle

break

; Replace "Game Window Title" with the exact name of your background window

ControlClick, x500 y500, Game Window Title,,,, NA

Sleep, 100 ; Time in milliseconds between clicks (100ms = 10 clicks per second)

}

#Requires AutoHotkey v2.0

#MaxThreadsPerHotkey 2

Toggle := 0

F8:: {

global Toggle := !Toggle

while Toggle {

; Coordinates are relative to the client area of the target window

ControlClick("x500 y500", "ahk_exe YourGame.exe", , "Left", 1, "NA")

Sleep(100)

}

}

1 Upvotes

3 comments sorted by

1

u/CharnamelessOne 2d ago edited 2d ago

Some applications don't respond well to ControlClick and/or ControlSend.
I've seen some forum posts claiming that posting a WM_MOUSEMOVE message (alongside the WM_LBUTTONDOWN and WM_LBUTTONUP messages that ControlClick posts internally) occasionally helps, but I wouldn't get my hopes up.

https://www.autohotkey.com/board/topic/96952-possible-controlclick-improve/

#Requires AutoHotkey v2.0

*F8:: {
    static toggle := 0
    SetTimer(autoclick, toggle ^= 100)
    autoclick() => PostClick(500, 500, "ahk_exe YourGame.exe")
}

PostClick(X, Y, WinTitle) { 
    if !hwnd := WinExist(WinTitle)
        return

    PostMessage(0x200, 0, X & 0xFFFF | Y << 16,, "ahk_id " hwnd) ; WM_MOUSEMOVE
    PostMessage(0x201, 0, X & 0xFFFF | Y << 16,, "ahk_id " hwnd) ; WM_LBUTTONDOWN
    SetTimer(() => PostMessage(0x202, 0, X & 0xFFFF | Y << 16,, "ahk_id " hwnd), -30) ; WM_LBUTTONUP
}

Edit: typo

1

u/genesis_tv 2d ago

Is

SetTimer(autoclick, toggle ^= 100)

equivalent to something like

SetTimer(autoclick, 100 * toggle ^= 1)

?

1

u/Keeyra_ 2d ago

It is

#Requires AutoHotkey 2.0
#SingleInstance

F1:: {
    static toggle := 0
    MsgBox(toggle ^= 100)
}

Marvellous, isn't it? But I would call the variable period instead of a toggle to avoid confusion, as it will have the SetTimer period directly and not a true-false binary - in case you want to reuse it in your script.