GetKeyState issue

Ask gaming related questions (AHK v1.1 and older)
whismerhill
Posts: 5
Joined: 18 Feb 2018, 12:24

GetKeyState issue

18 Feb 2018, 15:25

so I have done a bit of code I use in several different contexts

Code: Select all

~$XButton1::
    While GetKeyState("XButton1", "P")
    {
        Click Down
        Sleep 15
        Click Up
        Sleep 20  ;  milliseconds
    }
    Click Up
    sleep 50
    Click Up
Return
as you can see at first glance I added several click up to prevent clicks staying down for some unknown reason
the second thing :
it seems sometimes GetKeyState will fail to see that Xbutton1 has been released.

so is there a more efficient way of doing this ?
thanks
User avatar
divanebaba
Posts: 805
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: GetKeyState issue

18 Feb 2018, 16:21

In my opinion, following code should be enough

Code: Select all

~$XButton1::
    While GetKeyState("XButton1", "P")
    {
        Click Down
        Sleep 15
        Click Up
        Sleep 20  ;  milliseconds
    }
Return
I can not see any problems this code could generate.
So, more efficient is to use without any commands outside the braces.
Einfach nur ein toller Typ. :mrgreen:
whismerhill
Posts: 5
Joined: 18 Feb 2018, 12:24

Re: GetKeyState issue

18 Feb 2018, 16:48

yeah those were debug added commands because sometimes it would get stuck for some reason
but actually I'm not sure if it's the click getting stuck to down and not getting released by "up" or if it's the getkeystate that fails to register the change of state from xbutton1 to "released"
icuurd12b42
Posts: 202
Joined: 14 Aug 2016, 04:08

Re: GetKeyState issue

18 Feb 2018, 17:38

I strongly advise people stop using this sort of loop. make a timer

Example concept only. not tested

SetTimer(OnTimer, 20)
OnTimer()
{
if(GetKeyState("XButton1", "P"))
{
Click Down
Sleep 15
Click Up
}
}
whismerhill
Posts: 5
Joined: 18 Feb 2018, 12:24

Re: GetKeyState issue

18 Feb 2018, 20:37

icuurd12b42 wrote:I strongly advise people stop using this sort of loop. make a timer

Example concept only. not tested

SetTimer(OnTimer, 20)
OnTimer()
{
if(GetKeyState("XButton1", "P"))
{
Click Down
Sleep 15
Click Up
}
}
alrighty, so you strongly advise people to stop using this sort of loop, but why ?
what is this code supposed to address ?

pardon me for my inexperience, all I see here is a timer that'll run continuously every 20ms, it doesn't seem to improve anything but maybe I'm wrong for all I know ???

Thanks for your input either way.
User avatar
divanebaba
Posts: 805
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: GetKeyState issue

18 Feb 2018, 20:51

For some issues, the using of a timer is necessary or at least best solution and sometimes, as icuurd12b42 says, strongly recommended.
In this case, I mean, using a timer is not necessary, because when the loop is passed, the keystate will be checked accurately.
Using timer solutions is, to avoid using of sleep command which prevents recognizing keystrokes, while the script sleeps (is frozen).
So if you have something with toggle and you want to change a variable by hotkey or keystroke a sleep command can prevent toggle function, while it freezes the script.
Einfach nur ein toller Typ. :mrgreen:
whismerhill
Posts: 5
Joined: 18 Feb 2018, 12:24

Re: GetKeyState issue

20 Feb 2018, 18:54

I'm not quite 100% sure but it seems to not get stuck if code is organized like this instead of the first post :

Code: Select all

Clicking()
{
	While GetKeyState("XButton1", "P")
	{
		Click Down
		Sleep 15
		Click Up
		Sleep 20  ;  milliseconds
	}
}

XButton1::
	Clicking()
Return
hence not quite sure if it's the function or the removal of the modifiers but it just seems to work better (so far not once got stuck)

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 37 guests