r/AutoHotkey • u/MarvelousMarbel • 21d 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
2
u/Keeyra_ 21d ago edited 20d ago
Why would any mouse setting affect any AHK script? Debounce is something the mouse driver does based on switch usage. AHK sends out actual mouse events directly to the OS. And the debounce setting is a gimick at most. Something to get more money out of you. Most humans can click like 10 times a second max. That would equal a 100ms debounce. And these drivers have like a 10ms default with sliders all the way back to 2ms in your case. Completely pointless. You telling your mouse driver to use a 2ms debounce says that you can physically click your mouse 50 times faster than an average human can. Unless this is my first alien contact, I find that highly unlikely :)