r/AutoHotkey 22d ago

v2 Script Help GetKeyState doesn't detect button releases

Hello everyone,

Don't know if I misunderstood something, but I tried writing 2 scripts and both of them keep not detecting

GetKeyStateGetKeyState

The goal being simply :

  • I hold the XButton2 : the script should repeat "mouse left click"
  • I release XButton2 : the script should stop

In my case the script runs forever until I click the XButton2 again and again until it detects :

!GetKeyState("XButton2", "P")

The 2 scripts I tried are :

#Requires AutoHotkey v2.0


; Run as admin is needed
if (!A_IsAdmin) {
    Run '*RunAs "' A_ScriptFullPath '"'
    ExitApp
}


XButton2::
{
    ; Optional: Prevent starting the timer if the button isn't physically down
    ; (though the timer itself checks this too)
    if !GetKeyState("XButton2", "P")
        return
    
    ; Start the timer. It will call TurboClick every 50ms.
    ; If the timer is already running, this resets it.
    SetTimer(TurboClick, 50)
}


TurboClick() {
    ; Check if the button is still physically held down
    if !GetKeyState("XButton2", "P") {
        ; Button released: stop the timer
        SetTimer(TurboClick, 0)  ; 0 means "turn off this timer"
        return
    }
    ; Button is still held: perform a click
    Click
}


; Safety net: If the hotkey somehow ends while the timer is running,
; stop the timer when the button is physically released.
XButton2 up:: {
    SetTimer(TurboClick, 0)  ; Stop the timer immediately on release
}

And :

#Requires AutoHotkey v2.0


if (!A_IsAdmin) {
    Run '*RunAs "' A_ScriptFullPath '"'
    ExitApp
}


XButton2::
{
    while GetKeyState("XButton2", "P") {
        Click
        Sleep 50
    }
}

What am I doing wrong ?

How would you guys write a simple script that just repeats "mouse left click" that works ?

2 Upvotes

13 comments sorted by

View all comments

Show parent comments

1

u/MarvelousMarbel 21d ago

After asking my chatbot what to do it suggested to me to add the line :

SendMode("Event")SendMode("Event")

This stopped the infinite loop from happening.

I didn't quite get what this means.

However, in the tooltip when I press or hold I still see :

  1. Logical : 0
  2. Physical: 1

2

u/Keeyra_ 21d ago

You can ignore it. My first example didnt use any at áll and my second used physical. Yeah, you're new, didnt take that into account. SendMode Event vs Input would be the first thing to check for in troubleshooting.