Need help with a Loop while spamming key Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
NCat1234
Posts: 2
Joined: 02 Jun 2023, 18:31

Need help with a Loop while spamming key

Post by NCat1234 » 02 Jun 2023, 18:52

What I want the program to do:
- I press F8, it holds down a (if you have a notepad open it would spam aaaaaaaaaaaaaa)
- while it is spamming "a", I want it to click back and forth between my x1y1 and x2y2, 62 times, followed by a click on x3y3 and x4y4, then repeat 7 times
- If i remove the Send a up/down it does everything correctly

I know "a down" and "a up" is not the correct thing to put in here but it's just there to help explain what I want to happen. I've been looking on this forum and online for answers and have not gotten any progress. Any help is greatly appreciated!


Code: Select all

#MaxThreadsPerHotkey 4
CoordMode Pixel, window
CoordMode Mouse, window

toggle := 0

F8::
toggle += 1

if (toggle = 1)	{

	loop, 7 { 

	Random, x1, 1400, 1516
	Random, y1, 909, 978

	Random x2, 1590, 1695
	Random y2, 442, 515

	Random x3, 1013, 1044 
	Random y3, 722, 749

	Random x4, 865, 891
	Random y4, 583, 613

	Send {a down}
	
		loop, 62 {
			MouseClick, left, x1, y1,,,, 
			Sleep, 200
			MouseClick, left, x2, y2,,,,
			Sleep, 200
		}

	Send {a up}
	Sleep, 200
	MouseClick, left, x3, y3,,,, 
	Sleep, 200
	MouseClick, left, x4, y4,,,,
	Sleep, 200
	}
}

F9::reload

F10::exitapp
[Mod edit: [code][/code] tags added.]

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

Re: Need help with a Loop while spamming key  Topic is solved

Post by mikeyww » 02 Jun 2023, 19:49

Welcome to this AutoHotkey forum!

Code: Select all

#Requires AutoHotkey v1.1.33
#MaxThreadsPerHotkey 2
Global on

F8::
If on := !on {
 SetTimer spam, 15
 Loop 7 {
  Random x1, 1400, 1516
  Random y1, 909, 978
  Random x2, 1590, 1695
  Random y2, 442, 515
  Random x3, 1013, 1044
  Random y3, 722, 749
  Random x4, 865, 891
  Random y4, 583, 613
  Loop 62
   spam2(x1, y1), spam2(x2, y2)
  Until !on
  spam2(x3, y3), spam2(x4, y4)
 } Until !on
} Else SetTimer spam, Off
Return

spam() {
 Send a
}

spam2(x, y) {
 MouseClick,, x, y, on, 0
 end := A_TickCount + 200
 While (on && A_TickCount < end)
  spam()
}

NCat1234
Posts: 2
Joined: 02 Jun 2023, 18:31

Re: Need help with a Loop while spamming key

Post by NCat1234 » 03 Jun 2023, 08:39

Thank you so much, this helped me get it to work. I've learned a few new things from your response, much appreciated!
mikeyww wrote:
02 Jun 2023, 19:49
Welcome to this AutoHotkey forum!

Code: Select all

#Requires AutoHotkey v1.1.33
#MaxThreadsPerHotkey 2
Global on

F8::
If on := !on {
 SetTimer spam, 15
 Loop 7 {
  Random x1, 1400, 1516
  Random y1, 909, 978
  Random x2, 1590, 1695
  Random y2, 442, 515
  Random x3, 1013, 1044
  Random y3, 722, 749
  Random x4, 865, 891
  Random y4, 583, 613
  Loop 62
   spam2(x1, y1), spam2(x2, y2)
  Until !on
  spam2(x3, y3), spam2(x4, y4)
 } Until !on
} Else SetTimer spam, Off
Return

spam() {
 Send a
}

spam2(x, y) {
 MouseClick,, x, y, on, 0
 end := A_TickCount + 200
 While (on && A_TickCount < end)
  spam()
}

Post Reply

Return to “Ask for Help (v1)”