execute string only as long as a button is pushed down?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
TheQuestions

execute string only as long as a button is pushed down?

23 Jul 2017, 06:24

Hi. Im trying to make a script that doesn't loop, but just execute one set of inputs in a specific order/timing while holding down a key.

Exemplae:

I want my script to type the following:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

But only as long as Im holding down the F1 key, and if I release the F1 key before the whole string is completed the script should just cut of at that point and not execute the lines that comes after that.

Example:

If I hold down the F1 key 10sec the full string should look like this:
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

But if I only hold down F1 for 5sec it should only look like this:
A B C D E F G H I J K L M N

And if i only hold down the F1 key for 2 sec it should look like this:
A B C D E F

I'm sorry for the lengthy description, but this was the only good way I could think of to describe what I'm looking for.
I did try searching for similar threads and wasn't able to find anything. I also visited the FAQ but had no luck finding what I was looking for there as well.

Hope someone can help me on this one.

//Thanks in advanced.
User avatar
boiler
Posts: 16929
Joined: 21 Dec 2014, 02:44

Re: execute string only as long as a button is pushed down?

23 Jul 2017, 07:27

Make a F1 hotkey that loops until GetKeyState shows that it is released. Inside that loop, send the next character in your string after the desired fraction of a second has passed.
crimson4649
Posts: 18
Joined: 23 Jul 2017, 06:04

Re: execute string only as long as a button is pushed down?

23 Jul 2017, 07:53

Its me the creator of the topic. saw I wasn't logged in :P

Ok.

this is how fare I have gotten.

Code: Select all

F1::
SetKeyDelay, -1
While GetKeyState("F1")
{
Send, {A down}
Sleep 5
Send, {A up}
Sleep 1000
Send, {B down}
Sleep 5
Send, {B up}
Sleep 1000
Send, {C down}
Sleep 5
Send, {C up}
Sleep 1000
Send, {D down}
Sleep 5
Send, {D up}
Sleep 1000
Send, {E down}
Sleep 5
Send, {E up}
Sleep 1000
Send, {F down}
Sleep 5
Send, {F up}
Sleep 1000
Send, {G down}
Sleep 5
Send, {G up}
Sleep 1000
Send, {H down}
Sleep 5
Send, {H up}
Sleep 1000
}
Return
What am i missing/need to do?
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: execute string only as long as a button is pushed down?

23 Jul 2017, 08:11

That looks tedious. Try something like:

Code: Select all

F1::
	String=ABCDEFGHIJKLMNOPQRSTUVWXYZ
	SetKeyDelay,-1,5 ;SetKeyDelay 1000 instead of Sleep 1000 nags about #MaxHotkeysPerInterval, not sure why
	While GetKeyState("F1","P"){
		Send % SubStr(String,Mod(A_Index,StrLen(String)),1)
		Sleep 1000
	}
Return
User avatar
boiler
Posts: 16929
Joined: 21 Dec 2014, 02:44

Re: execute string only as long as a button is pushed down?

23 Jul 2017, 08:32

Yeah, crimson, not only is Nextron's code way more efficient, yours doesn't check to see if the key was released after each character. You also don't need to send a down and up for every key you send, especially since a capital letter is not a key, it's a character that involves a shift, so your approach wouldn't work.
crimson4649
Posts: 18
Joined: 23 Jul 2017, 06:04

Re: execute string only as long as a button is pushed down?

23 Jul 2017, 08:45

the ABC string was just en example. I need to execute something much more complex that requires me to use the key down/key up command, I also need to have variable delays in between each command.

This is the actuale code:

Code: Select all

F1::
SetKeyDelay, -1
While GetKeyState("F1")
{
Send, {s down}
Sleep 22
Send, {s up}
Sleep 122
Send, {a down}{s down}{j down}
Sleep 22
Send, {a up}{s up}{j up}
Sleep 100
Send, {s down}
Sleep 22
Send, {d down}
Sleep 22
Send, {s up}{u down}
Sleep 22
Send, {d up}{u up}
Sleep 320
Send, {u down}{i down}
Sleep 22
Send, {u up}{i up}
Sleep 22
Send, {d down}
Sleep 22
Send, {d up}
Sleep 22
Send, {d down}
Sleep 22
Send, {d up}
Sleep 200
Send, {a down}{u down}
Sleep 22
Send, {a up}{u up}
Sleep 22
Send, {d down}
Sleep 22
Send, {d up}
Sleep 22
Send, {s down}
Sleep 22
Send, {d down}
Sleep 22
Send, {s up}{k down}
Sleep 22
Send, {d up}{k up}
Sleep 628
Send, {j down}
Sleep 22
Send, {j up}
Sleep 470 ;;;; 520
Send, {d down}
Sleep 22
Send, {d up}
Sleep 22
Send, {s down}
Sleep 22
Send, {d down}
Sleep 22
Send, {s up}{k down}
Sleep 22
Send, {d up}{k up}
Sleep 500
Send, {j down}
Sleep 22
Send, {j up}
Sleep 470 
Send, {a down}{u down}
Sleep 22
Send, {a up}{u up}
Sleep 66
Send, {s down}
Sleep 22
Send, {a down}
Sleep 22
Send, {s up}{j down}
Sleep 22
Send, {a up}{j up}
Sleep 520
Send, {d down}
Sleep 22
Send, {d up}
Sleep 22
Send, {s down}
Sleep 22
Send, {d down}
Sleep 22
Send, {s up}{u down}
Sleep 22
Send, {d up}{u up}
Sleep 1180
Send, {d down}
Sleep 22
Send, {s down}
Sleep 22
Send, {d up}
Sleep 22
Send, {a down}
Sleep 22
Send, {s up}{i down}
Sleep 22
Send, {a up}{i up}
}
Return
User avatar
boiler
Posts: 16929
Joined: 21 Dec 2014, 02:44

Re: execute string only as long as a button is pushed down?

23 Jul 2017, 09:07

Then you'll have to store that info in an array and have a function that interprets it and executes it. The way you have it, it won't do the check for F1 being released in between each.
crimson4649
Posts: 18
Joined: 23 Jul 2017, 06:04

Re: execute string only as long as a button is pushed down?

24 Jul 2017, 04:00

boiler wrote:Then you'll have to store that info in an array and have a function that interprets it and executes it. The way you have it, it won't do the check for F1 being released in between each.
that is way above me, how do i do that?
btw. thanks for the help :)
Rohwedder
Posts: 7630
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: execute string only as long as a button is pushed down?

24 Jul 2017, 05:05

Hallo,
try this:

Code: Select all

F1::
Loop
{
	Send, {s down}
	SleepUntilRelease(-22)
	Send, {s up}
	SleepUntilRelease(122)
	Send, {a down}{s down}{j down}
	SleepUntilRelease(-22)
	Send, {a up}{s up}{j up}
	SleepUntilRelease(100)
	Send, {s down}
	SleepUntilRelease(-22)
	Send, {d down}
	SleepUntilRelease(-22)
	Send, {s up}{u down}
	SleepUntilRelease(-22)
	Send, {d up}{u up}
	SleepUntilRelease(320)
	Send, {u down}{i down}
	SleepUntilRelease(-22)
	Send, {u up}{i up}
	SleepUntilRelease(22)
	Send, {d down}
	SleepUntilRelease(-22)
	Send, {d up}
	SleepUntilRelease(22)
	Send, {d down}
	SleepUntilRelease(-22)
	Send, {d up}
	SleepUntilRelease(200)
	Send, {a down}{u down}
	SleepUntilRelease(-22)
	Send, {a up}{u up}
	SleepUntilRelease(22)
	Send, {d down}
	SleepUntilRelease(-22)
	Send, {d up}
	SleepUntilRelease(22)
	Send, {s down}
	SleepUntilRelease(-22)
	Send, {d down}
	SleepUntilRelease(-22)
	Send, {s up}{k down}
	SleepUntilRelease(-22)
	Send, {d up}{k up}
	SleepUntilRelease(628)
	Send, {j down}
	SleepUntilRelease(-22)
	Send, {j up}
	SleepUntilRelease(470) ;;;; 520
	Send, {d down}
	SleepUntilRelease(-22)
	Send, {d up}
	SleepUntilRelease(22)
	Send, {s down}
	SleepUntilRelease(-22)
	Send, {d down}
	SleepUntilRelease(-22)
	Send, {s up}{k down}
	SleepUntilRelease(-22)
	Send, {d up}{k up}
	SleepUntilRelease(500)
	Send, {j down}
	SleepUntilRelease(-22)
	Send, {j up}
	SleepUntilRelease(470)
	Send, {a down}{u down}
	SleepUntilRelease(-22)
	Send, {a up}{u up}
	SleepUntilRelease(66)
	Send, {s down}
	SleepUntilRelease(-22)
	Send, {a down}
	SleepUntilRelease(-22)
	Send, {s up}{j down}
	SleepUntilRelease(-22)
	Send, {a up}{j up}
	SleepUntilRelease(520)
	Send, {d down}
	SleepUntilRelease(-22)
	Send, {d up}
	SleepUntilRelease(22)
	Send, {s down}
	SleepUntilRelease(-22)
	Send, {d down}
	SleepUntilRelease(-22)
	Send, {s up}{u down}
	SleepUntilRelease(-22)
	Send, {d up}{u up}
	SleepUntilRelease(1180)
	Send, {d down}
	SleepUntilRelease(-22)
	Send, {s down}
	SleepUntilRelease(-22)
	Send, {d up}
	SleepUntilRelease(-22)
	Send, {a down}
	SleepUntilRelease(-22)
	Send, {s up}{i down}
	SleepUntilRelease(-22)
	Send, {a up}{i up}
}
Return

;like Sleep(Abs(Time))
;but when (one of) the Hotkey will be released, then
;if Time >= 0: the HotKey-Thread ends.
;if Time < 0: only this Sleep ends.
;A long sleep will be segmented to be able to interrupt it
SleepUntilRelease(Time)
{ 
	End:= A_TickCount + Abs(Time) - 10
	StringSplit,K,A_ThisLabel,&,*~+ ^!
	While, A_TickCount < End
	{		
		IF !GetKeyState(K1,"P") Or !GetKeyState(K%K0%,"P")
			IF Time < 0
				Break
			Else
				Exit
		Sleep,End-A_TickCount>100?100:End-A_TickCount
	}
}
crimson4649
Posts: 18
Joined: 23 Jul 2017, 06:04

Re: execute string only as long as a button is pushed down?

25 Jul 2017, 09:33

Thanks. it looks like it works. I need to re-adjust the sleep timers because there seem to be an extra delay now in between each string.
Btw. this is for a game that runs in 60fps. is there a way to make the sleep/tick counter count in 60fps instead of 100fps? (1frame = ~16,666... ms?)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Decar, doodles333, mikeyww and 214 guests