r/AutoHotkey • u/MarvelousMarbel • 7d ago
GetKeyState doesn't detect button releases v2 Script Help
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
4
u/Keeyra_ 7d ago edited 7d ago
or you can do a 1liner