Need help with key activation only if a press is held for a certain amount of time Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
1eet
Posts: 3
Joined: 21 May 2022, 00:35

Need help with key activation only if a press is held for a certain amount of time

Post by 1eet » 21 May 2022, 00:41

Code: Select all

1::
Loop
{
    if not GetKeyState("1", "P")
        break

send {1 down}
sleep, 50
send {1 up}
}
return
Basically what I want to do here is have a key that auto repeats its click (up,down,up,down) this part already works fine, now I have a problem. The key gets activated way too quickly so if I just press "1" it'll automatically go "11"

I tried looking around if there was a way to add some sort of condition that only activate IF "1" has been held for say 100ms. This must already exist somewhere here but I can't word it in a way that the search function understands...

TLDR, make it so > IF "1" is held for 100ms, then execute the loop

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

Re: Need help with key activation only if a press is held for a certain amount of time

Post by Rohwedder » 21 May 2022, 01:00

Hallo,
try:

Code: Select all

$1::
KeyWait, 1, T.1 ; 0.1 seconds
IF ErrorLevel ; > 0.1 seconds
	While GetKeyState("1","P")
	{
		send {1 down}
		sleep, 50
		send {1 up}
	}
Return

1eet
Posts: 3
Joined: 21 May 2022, 00:35

Re: Need help with key activation only if a press is held for a certain amount of time

Post by 1eet » 21 May 2022, 02:03

Sorry but this is not exactly what I was looking for, basically this does technically work but if you let go of the key too soon there will be no input.
What I'm trying to achieve here is to be able to type in "1" without the script interfering. My code does work, but if you press the key for morethan 30ms(if you don't let go fast enough) it'll basically write "11" I just wanted to add a bit of delay before AHK registers it as an input for the macro.

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

Re: Need help with key activation only if a press is held for a certain amount of time  Topic is solved

Post by Rohwedder » 21 May 2022, 02:39

Then,
try:

Code: Select all

$1::
Send 1
KeyWait, 1, T.3 ; 0.3 seconds
IF ErrorLevel ; > 0.3 seconds
	While GetKeyState("1","P")
	{
		send {1 down}
		sleep, 50
		send {1 up}
		sleep, 50
	}
Return

1eet
Posts: 3
Joined: 21 May 2022, 00:35

Re: Need help with key activation only if a press is held for a certain amount of time

Post by 1eet » 21 May 2022, 09:26

Thank you! this exactly did it!

Post Reply

Return to “Ask for Help (v1)”