Repeating multiple different keystrokes with differing delays for each...?

Ask gaming related questions
mojoz
Posts: 2
Joined: 22 Jan 2021, 22:58

Repeating multiple different keystrokes with differing delays for each...?

Post by mojoz » 22 Jan 2021, 23:05

After a lot of researching on here, I've worked out how to toggle a repeating keystroke with a random delay, but I can't for the life of me work out how to send multiple keys with different but parallel (ie not sequential) delays.

So, for example, when I toggle xbutton1, I want "2" and "3" pressed, and then for "2" to repeat around every 4.8 seconds, while "3" will repeat for around every 6.0 seconds - but I want these independent delays to run in parallel, not in sequence.

Any guidance greatly appreciated, thank you!

Code: Select all

#maxThreadsPerHotkey, 2
setKeyDelay, 25, 25 ;was 50, 50
setMouseDelay, 25 ;was 50
banana:=0
$XButton1::
	banana:=!banana
	while (banana=1)
	{
		send, 2
		Random ranSleep2, 4750, 4850                  
		sleep %ranSleep2%
	}
return
User avatar
Spawnova
Posts: 456
Joined: 08 Jul 2015, 00:12
Contact:

Re: Repeating multiple different keystrokes with differing delays for each...?

Post by Spawnova » 22 Jan 2021, 23:28

Code: Select all

#maxThreadsPerHotkey, 2
setKeyDelay, 25, 25 ;was 50, 50
setMouseDelay, 25 ;was 50
toggle := 0

keys := [] ;creates empy array
keys["2"] := {minDelay:4750,maxDelay:4850,currentDelay:0}  ;make a new array that hold the key press information
keys["3"] := {minDelay:5950,maxDelay:6050,currentDelay:0}
return


$f1::
	toggle:=!toggle
	settimer,sendKeys,% (toggle ? 10 : "off")
return

sendKeys:
	for k,v in keys ;loop through keys and check if they can be pressed
	{
		if (v.currentDelay < a_tickcount) {
			send % k
			v.currentDelay := a_tickcount + random(v.minDelay,v.maxDelay)
		}
	}
return

random(min,max:=100) {
	random,result,% min,% max
	return result
}
Instead of using sleeps, which pauses the whole program you can instead use a loop, in this case a timer label, and check the next time a key should be pressed
Some of my AHK programs: 3D Voxel Game - Platformer Game - Langton's Ant - Raycast light/Pixel Water - Creating HD map of any game
Or check out my Youtube for these plus many more projects! :D
mojoz
Posts: 2
Joined: 22 Jan 2021, 22:58

Re: Repeating multiple different keystrokes with differing delays for each...?

Post by mojoz » 22 Jan 2021, 23:36

Oh my goodness, that is so amazing, thank you very very much mate! I will see how this goes, I am deeply indebted to your thorough and fast response! Cheeers!
Post Reply

Return to “Gaming”