Hold/Toggle key script

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
helming
Posts: 25
Joined: 07 Dec 2016, 15:01

Re: Hold/Toggle key script

15 Oct 2017, 22:29

Woooooow! Thanks in million times Exaskryz!

How could I be so stupid!!!

Really Really appreciate your help!
Xailus
Posts: 1
Joined: 13 Jul 2020, 21:50

Re: Hold/Toggle key script

13 Jul 2020, 21:56

Hey can anyone make a script that holds 2 keys? I need a script that holds W for 7 Sec, then holds S for 7 sec also and the toggle is F1? Please help thanks
breadbox88
Posts: 1
Joined: 06 Feb 2016, 15:54

Re: Hold/Toggle key script

31 Mar 2021, 12:15

This script allows a single key to be used as either a regular key or to toggle itself on/off by holding down for about 1 second then clicking the button again to cancel the loop. It's not perfect but it works.

Code: Select all

$1::
	keyDwn = 1
	keyLoop = OFF
	Loop
	{
		sendInput 1
		sleep 250
		cFct_GetKeyState(keyDwn, keyEsc, keyLoop)
	} Until (keyLoop = "OFF")
	sendInput {F2 Up}
	sendInput {ENTER}
return
$1 up::
	If (keyEsc = "ON") && (keyLoop = "ON")
	{
		keyLoop = OFF
	}
return

cFct_GetKeyState(keyDwn, ByRef keyESC, ByRef keyLoop)
{
	GetKeyState, keyState, %keyDwn%, P
	if keyState = D
	{
		sleep 250	;second timer helps avoid accidently trigging the loop if spamming the key
		GetKeyState, keyState, %keyDwn%, P
		if keyState = D
		{
			keyESC = OFF
			keyLoop = ON
		}
	} else {
		keyEsc = ON
	}
}
helming
Posts: 25
Joined: 07 Dec 2016, 15:01

Re: Hold/Toggle key script

09 Mar 2022, 19:32

Hello, I have a question.


Code: Select all

#Maxthreadsperhotkey 2

f1::
	{
	Togloop := !Togloop
	while Togloop	{

		send	%Togloop%
		sleep	5000/(3-presscount)
		send	again
		sleep	5000
		Tooltip, %A_Index%

		}
	Tooltip
	
	Return
	}



f1 is used for toggling a command.

Pressing f1 will start the loop, pressing it again stop.

Below is a code which used settimer

Code: Select all


#Maxthreadsperhotkey 2

f3::
	{
	presscount++
	Settimer, testa, -800
	Return
	}
	
	
testa:	
	{
	Togloop := !Togloop
	Tooltip, %  (presscount = 1 ? "short" : "long")
	while Togloop	{

		send	%Togloop%
		sleep	5000/(3-presscount)
		send	again
		sleep	5000
		}
	Tooltip
	
	presscount:=0
	Return
	}


It seems pressing f3 again doesn't break the ongoing loop.
It increases the 'presscount' variable, but loop doesn't break.
My guess is 'testa' subrountine isn't allowed to have more than one instance, maybe because
maxthreadsperhotkey doesn't applied to subrountines?

Is that correct?

If so how can i work around this? :?:
prz
Posts: 6
Joined: 27 Nov 2023, 23:39

Re: Hold/Toggle key script

27 Nov 2023, 23:42

If you (like me) were struggling to find the "v2" version of this, there's a thread over here: viewtopic.php?f=82&t=122046

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: aitaixy, Anput, dangoscrub, Google [Bot], joedf, Nerafius and 144 guests