Loop sticking with "P"

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
SarahJ
Posts: 29
Joined: 01 Feb 2016, 11:18

Loop sticking with "P"

Post by SarahJ » 18 Jan 2022, 14:50

I have 2 kinds of loops currently. Ive narrowed down my button sticking in NW to those with Getkeystate("key" "P") and those without the "P". Problem is that only some of my keys will work without the "P". My left button on moue and my spacebar wont work without it so they stick all the time. Is there a way to get these keys to stop sticking or work without the "P"? Ive tried every loop type people have suggested already.
sample code:

Code: Select all

    space::
        while GetKeyState("space", "P")
        {
            send {space}
            sleep 200
        }
    return

Code: Select all

1::
gosub, one
return
one:
        if getkeystate("1")
        {
        send 1
            settimer, one, -200
        }
    return

User avatar
mikeyww
Posts: 26598
Joined: 09 Sep 2014, 18:38

Re: Loop sticking with "P"

Post by mikeyww » 18 Jan 2022, 15:13

If you send the same key as the hotkey, prefix the hotkey with a dollar sign to prevent the loop. Various flavors are below.

Code: Select all

$Space::
SetKeyDelay, 200
Send {Space}
Return

Code: Select all

#UseHook
Space::
1::
SetKeyDelay, 200
Send {%A_ThisHotkey%}
Return
#UseHook Off

Code: Select all

SetKeyDelay, 200
#UseHook
Space::
1::Send {%A_ThisHotkey%}
#UseHook Off

Code: Select all

SetKeyDelay, 200
$Space::
$1::Send % "{" SubStr(A_ThisHotkey, 2) "}"
Sleep is also OK.

Code: Select all

$Space::
Send {Space}
Sleep, 190
Return

SarahJ
Posts: 29
Joined: 01 Feb 2016, 11:18

Re: Loop sticking with "P"

Post by SarahJ » 20 Jan 2022, 03:41

its supposed to loop while the key is held down. But the loop keeps going sometimes when I let up on the key. In KeyHistory it shows the key down the whole time. This only applies to GetKeyState loops with the "P" setting though.

User avatar
mikeyww
Posts: 26598
Joined: 09 Sep 2014, 18:38

Re: Loop sticking with "P"

Post by mikeyww » 20 Jan 2022, 04:57

You can post your revised script here. Please note my comment about the dollar prefix, which applies the hook. I included a citation to an explanation of it. It would also be useful to know whether you tried any of the scripts that I posted, and whether they work.

Here is an example that you can adapt. viewtopic.php?p=439864#p439864 You can remove the + if you do not need to use Shift.

Rohwedder
Posts: 7551
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Loop sticking with "P"

Post by Rohwedder » 20 Jan 2022, 07:20

Hallo,
perhaps?:

Code: Select all

Global Repeat := []

$Space::Repeat(200)
$1::Repeat(200)

Repeat(Time:=30) {
	RegExMatch(A_ThisHotkey, "\W$|\w*$", Key) 
	IF !Repeat[Key] {
	Repeat[Key] := True
	Timer := Func("Timer").Bind([Key,-Max(Abs(Time),1)])
	SetTimer,% Timer,% -10
}}
Timer(B) {
	IF Repeat[B.1] := GetKeyState(B.1, "P") {
	SendInput,% "{Blind}{" B.1 "}"
	Timer := Func("Timer").Bind(B)
	SetTimer,% Timer,% B.2	
}}

SarahJ
Posts: 29
Joined: 01 Feb 2016, 11:18

Re: Loop sticking with "P"

Post by SarahJ » 21 Jan 2022, 00:07

I had read through the link and im pretty sure Im using the hook in the entire script. I will try adding the dollar sign and using setkeydelay. Trying the space bar one to see if it sticks and ill replace all my buttons if it doesnt. My mouse unfortunately is one of the ones that sticks. And from what I understand the mouse always uses the hook. These are in the script.

#InstallKeybdHook
#InstallMouseHook

User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Loop sticking with "P"

Post by Xtra » 21 Jan 2022, 00:59

You don't need to use GetKeyState().

Example:

Code: Select all

$space::
    running := 1
    Loop
    {
        Send, {space}
        Sleep 200
    }until !running
return

$space up::running := 0

SarahJ
Posts: 29
Joined: 01 Feb 2016, 11:18

Re: Loop sticking with "P"

Post by SarahJ » 21 Jan 2022, 21:30

ah ill try this with the Key up thanks :)

Post Reply

Return to “Ask for Help (v1)”