Help w/ Making a Script

Ask gaming related questions (AHK v1.1 and older)
Alucxrd
Posts: 3
Joined: 15 Jan 2018, 05:28

Help w/ Making a Script

15 Jan 2018, 05:41

I was wondering how you created the type of script that looped an action until you finally pressed a key to stop it. Specifically one that would grind for you without you having to do anything. I'm not very good at coding though :trollface: so I wouldn't know how to create this type of script. :terms:
My idea is that the script presses the same key repeatedly and then holds down a key, then loops this process. I've tried it but it just ended up shaking my screen and closing the game I tried to do it on. :facepalm:
Please help me, AHK Community! :D
User avatar
eventhorizon
Posts: 158
Joined: 27 Oct 2016, 14:22

Re: Help w/ Making a Script

16 Jan 2018, 02:15

here is an example of how it might be done... this uses an untitled windows notepad window as the target window. I added the comments so you could see what the thought process was behind each statement. Scripting is not a spectator sport though. You have to actually think it through, try to program it, and get your hands dirty to learn. Mistakes are not a bad thing cause they show you what works and what doesn't.

Code: Select all

tgtwin = Untitled ;<- for the notepad window
settitlematchmode,2 ;<- so you don't need the whole nopad window title
detecthiddenwindows On ;<- so the script can detect the target window even if hidden
return


; example: loop the 1 key press 1 to start looping and press again to stop looping
; this script uses an untitled windows notepad window as the target to send the 1 key
; to. if the window becomes inactive or the 1 key is pressed again the script stops
; temporarily until the 1 key is pressed again and the notepad window is active
; to stop the script entirely press the esc key
; the message box at the end and the tooltip windows are there to let you know what
; the script is doing as it does it.
$1::
	keywait, 1
	looping := !looping ;<- toggle for looping the key
	tooltip, Line%A_LineNumber% hotkey running. (looping = %looping%)...

	while (looping) ;<- if looping is on
	{	ifwinnotactive, %tgtwin% ;< if the notepad window is not active
		{	looping = 0 ;<- set looping off
			break ;<- and exit the loop
		}
		
		if (getkeystate("1","p")) ;<-if the one key is pressed again inside the loop
		{	keywait 1 ;<- wait til the key is released
			looping = 0 ;<- turn looping off
			break ;<- and exit the loop
		}
		
		send 1 ;<- send the 1 key to the active window
		sleep 200 ;<- sleep delay 2/10ths of a second
	}
	tooltip, Line%A_LineNumber% hotkey ended. (looping = %looping%)...
	return ;<- end the loop but stay ready for more

esc:: ;<- exits the script entirely
	tooltip ;<- turns off the tooltip message
	MsgBox,0x1000,Line%A_LineNumber%,Script Ended. ;<- signals the end of the script
	ExitApp
	
A computer lets you make more mistakes faster than any invention in human history – with the possible exceptions of handguns and tequila.
Alucxrd
Posts: 3
Joined: 15 Jan 2018, 05:28

Re: Help w/ Making a Script

16 Jan 2018, 06:36

Yes, and I am starting to work on scripting a bit more than before. At first I was just kinda messing around with block coding, but now I've figured that it could be essential for my future so I'm going to start learning JavaScript in my freetime. Thank you for your input and help, eventhorizon.

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 112 guests