Could someone please convert my macro into an AHK script that only uses ControlSend commands.

Ask gaming related questions
gaveitatry
Posts: 13
Joined: 12 Feb 2023, 17:37

Could someone please convert my macro into an AHK script that only uses ControlSend commands.

Post by gaveitatry » 26 Mar 2023, 00:15

ORIGINAL MACRO BELOW

Program:
gamename.exe

Window Title:
Game Name


PART 1

Hotkey:
Shift + Control + [

Loop Start (infinite)

Pause 0.5 seconds
Press F7

Pause 0.5 seconds
Press F8

Pause 2.5 seconds
Click Left Mouse Button

Pause 0.04 seconds
Press Space

Pause 0.04 seconds
Press Space

Pause 4.0 seconds
Press F8

Pause 4.0 seconds
Press F8

Pause 4.0 seconds
Press F8

Pause 4.0 seconds
Press F8

Loop End


PART 2

Hotkey:
Shift + Control + ]

Stop what PART 1 was doing.


Explanation:
I am using this macro to spectate players in a game by committing suicide every 4 seconds. The mouse left click and the spacebar presses are to change the camera view and to follow a different player every 20 or so seconds, since some players are AFK and just stand still too long.

I tried to make a command that did all of this by editing a game file, but it kept exiting me to the main menu anytime the map changed. So I started using a macro instead. But the bad thing about the macro is that it needs the game window to be activated and in front, which prevents me from doing stuff in other windows since it needs to do something in the game window every 4 seconds. Then a developer friend of mine told me that AHK has something called ControlSend that may or may not work (50% chance) and that it could send all these keyboard presses to the game window without actually activating the game window and bringing it to the front.

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

Re: Could someone please convert my macro into an AHK script that only uses ControlSend commands.

Post by mikeyww » 26 Mar 2023, 10:56

Hello,

You are using this macro? Seriously? Is it, like, a ChatGPT program that runs the macro?

You are starting with a long and complicated script. You write this long script, run it, it does not work, and then you're not sure what to do next. I recommend starting with a one-line hotkey subroutine. You can then determine whether that individual line works. If it doesn't work, there is no point adding 20 more lines and a loop to it. An example is below. You can see if it works in Notepad. If so, you could then adjust it to try to get it to work in your game. After it works, you can expand your script.

Code: Select all

#Requires AutoHotkey v2.0
target := 'ahk_exe notepad.exe'

#HotIf WinExist(target)
F3::ControlSend 123, 'Edit1'
#HotIf
Your friend can also help you to troubleshoot this. If your friend will not help, what type of friend is it? Robots can be friendly but get fidgety sometimes. Get new friends! :)

gaveitatry
Posts: 13
Joined: 12 Feb 2023, 17:37

Re: Could someone please convert my macro into an AHK script that only uses ControlSend commands.

Post by gaveitatry » 26 Mar 2023, 16:22

Thanks for helping me get started.
mikeyww wrote:
26 Mar 2023, 10:56

Code: Select all

#Requires AutoHotkey v2.0
target := 'ahk_exe notepad.exe'

#HotIf WinExist(target)
F3::ControlSend 123, 'Edit1'
#HotIf

I lied. I don't have any friends. Rather, he is a developer of a program that I use. And sometimes I ask questions not related to his program on his program's website forum.

I had no idea what I was doing. I just kept doing trial and error a billion times until something worked. I was able to get a single line of code to work. Now that I know that the one line of code works, I'm sure all the other lines of code will work too because all I would need to do is subsitute the key sent (example: {Space}) with a different key like {F8}.


This is what is working for me...

Code: Select all

#Requires AutoHotkey v2.0

target := 'ahk_id 1116750'

^+[::ControlSend '{Space}', 1116750



But I am stuck now.

(1) I want the ‎‎Ctrl + Shift + [‎‎ hotkey to do a bunch of commands, like below, but I don't know how.

Code: Select all

^+[::

ControlSend '{Space}', 1116750

Sleep, 1000

ControlSend '{Space}', 1116750

Sleep, 4000

ControlSend '{F8}', 1116750

(2) Then I want to put it in a loop, like this.

Code: Select all

^+[::

Loop Start (infinite)

ControlSend '{Space}', 1116750

Sleep, 1000

ControlSend '{Space}', 1116750

Sleep, 4000

ControlSend '{F8}', 1116750

Loop End

(3) And finally, I want a ‎‎Ctrl + Shift + ]‎‎ hotkey to end the aforementioned loop.


Could you help me with that?

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

Re: Could someone please convert my macro into an AHK script that only uses ControlSend commands.

Post by mikeyww » 26 Mar 2023, 19:41

Hmm. Interesting. So you are saying that the following script works, right?

Code: Select all

#Requires AutoHotkey v2.0
target := 'ahk_id 1116750'
^+[::ControlSend '{Space}', 1116750
Questions for you to answer, please:
1. Did you write this script?
2. Where did you get the number 1116750?
3. What is your target program or window?
4. What happens when you run your script?

gaveitatry
Posts: 13
Joined: 12 Feb 2023, 17:37

Re: Could someone please convert my macro into an AHK script that only uses ControlSend commands.

Post by gaveitatry » 26 Mar 2023, 20:20

mikeyww wrote:
26 Mar 2023, 19:41
0. So you are saying that the following script works, right?
1. Did you write this script?
2. Where did you get the number 1116750?
3. What is your target program or window?
4. What happens when you run your script?
Yeah, this is my first time doing anything AHK, so it's a miracle that it works.

Originally, I used your target := 'ahk_exe notepad.exe', but it wouldn't work. Somehow I accidentally found a page on alternatives to ahk_exe. https://www.autohotkey.com/docs/v2/misc/WinTitle.htm. I wanted to try ahk_id, and I saw that the AutoHotKey 2.0 folder has a WindowSpy ahk that will tell you the ahk_id of a program. But even after I found it, it still didn't work. I only guessed it had something to do with the Edit1 and that I had to replace it with something else. I had no idea how to use or find the other control parameters on https://www.autohotkey.com/docs/v2/lib/Control.htm, but I just kept trying different things. At first I tried 'Omitted' but that definitely didn't work and needs a Window Title anyway, and I wanted to use an ID. So I figured it had to be HWND. I tried 'HWND 1116750', 'HWND (1116750)', and neither worked. When I tried just '1116750' it worked great.

Yeah, I created the script, but not really. I mean i just copied your script and changed two or three things.

My target program is a game with the ahk_id 1116750. I somehow figured out that by using WindowSpy when inside the game window.

When I run my script, I can be in any other window and press Ctrl Shift [ and it will cause my character to jump in the inactive game window (spacebar is used for jump).


So now, I want to do the things I mentioned two posts ago, with the using the one and only one hotkey to initiate a bunch of ControlSends, with pauses in between. Then after that, put it in a loop function. And then after that, making another hotkey to end the loop. The first step is to use one and only one hotkey (Ctrl Shift [) to initiate a bunch of ControlSends, with pauses in between. But anytime I try to take ^+[ apart from ^+[::ControlSend '{Space}', 1116750, or add more stuff like more ControlSends or even just a single pause (sleep, 5000) it breaks. So no idea how to get one hotkey to do a bunch of stuff. It's like the hotkey can only be used for each ControlSend, and that doesn't work for me. I don't want 20 ControlSends each with a different hotkey. That's why I'm asking for help, because I figure someone here might have a few weeks, months, years, or decades more knowledge and experience on how I can get this next step going.

gaveitatry
Posts: 13
Joined: 12 Feb 2023, 17:37

Re: Could someone please convert my macro into an AHK script that only uses ControlSend commands.

Post by gaveitatry » 26 Mar 2023, 20:27

I forgot to mention that if you want to ControlSend stuff like Space, Enter, F1-12, etc. you need to put it in brackets {}, but not only that, the brackets must be between single quotes or it won't work. That's why I had '{Space}' instead of just {Space}. Complete list of keys you can send are here https://www.autohotkey.com/docs/v1/lib/Send.htm.

User avatar
boiler
Posts: 16900
Joined: 21 Dec 2014, 02:44

Re: Could someone please convert my macro into an AHK script that only uses ControlSend commands.

Post by boiler » 26 Mar 2023, 20:53

gaveitatry wrote: I forgot to mention that if you want to ControlSend stuff like Space, Enter, F1-12, etc. you need to put it in brackets {}, but not only that, the brackets must be between single quotes or it won't work. That's why I had '{Space}' instead of just {Space}. Complete list of keys you can send are here https://www.autohotkey.com/docs/v1/lib/Send.htm.
FYI — You don’t need to inform mikeyww of that. He wasn’t asking you questions to learn about how it works. He was asking you questions to guide you down the correct path, just as he’ll tell you why your HWND that worked this time won’t work in the future.

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

Re: Could someone please convert my macro into an AHK script that only uses ControlSend commands.

Post by mikeyww » 26 Mar 2023, 20:58

That is all interesting.

Below is an example of how you can add one increment (additional step) to your script, so that you can test a loop.

Code: Select all

#Requires AutoHotkey v2.0
^+[:: {
 Loop 3 {
  ControlSend '{Space}', 1116750
  Sleep 1000
 }
}
In the long run, hard-coding your HWND will not work, because it will change. Window Spy shows you the window class and process name, so you could use one or both of them, retaining the ahk_... prefix.

EDIT: boiler knows the truth! :)

Concepts here:
1. A hotkey subroutine is a function and so is bound by braces.
2. A loop can use a code block, which is bound by braces.
3. V2 is based on functions rather than commands. As such, a function name is never followed by a comma, though if the first parameter is omitted, the function name may be followed by a space and then a comma. Using the traditional parentheses to enclose parameters can also be done, and is required when the line contains more than the sole function call.
4. A Loop may contain the number of iterations, or may omit the number to achieve continuous iteration.

You probably now have enough information to finish your script.

I was unaware that both the control parameter and its comma could be omitted-- not explicitly stated or shown in the documentation-- but you seem to have demonstrated that in your script.

Explained: The control parameter

gaveitatry
Posts: 13
Joined: 12 Feb 2023, 17:37

Re: Could someone please convert my macro into an AHK script that only uses ControlSend commands.

Post by gaveitatry » 27 Mar 2023, 13:21

mikeyww wrote:
26 Mar 2023, 20:58

Code: Select all

#Requires AutoHotkey v2.0
^+[:: {
 Loop 3 {
  ControlSend '{Space}', 1116750
  Sleep 1000
 }
}
Concepts here:
1. A hotkey subroutine is a function and so is bound by braces.
2. A loop can use a code block, which is bound by braces.
3. V2 is based on functions rather than commands. As such, a function name is never followed by a comma, though if the first parameter is omitted, the function name may be followed by a space and then a comma. Using the traditional parentheses to enclose parameters can also be done, and is required when the line contains more than the sole function call.
4. A Loop may contain the number of iterations, or may omit the number to achieve continuous iteration.

I was unaware that both the control parameter and its comma could be omitted-- not explicitly stated or shown in the documentation-- but you seem to have demonstrated that in your script.

Explained: The control parameter

Thank you for your help. The script is finally working now.

I was stuck for a long time this morning, trying a billion things that didn't work, and then I finally figured out that I needed this bit of code:

#HotIf WinExist(target)
#HotIf

Before, I thought that it was just a commented out section and wasn't necessary.

After re-adding it, it fixed a ton of things. My target
target := 'ahk_exe gamename.exe'
finally worked for the first time. Before it didn't, because I wasn't using the #HotIf commands, and that's why I was doing trial and error until the ahk_id thing worked (but only temporary since PIDs and IDs change every time the program is relaunched).

1.) I couldn't figure out how to make ControlClick work. I wanted to primary attack or left click once, anywhere in the gamename.exe window, every so often. But I found a good workaround. The game has keybinds, and I assigned an alternate key to the primary attack. So the key assigned to the primary attack is LMB, but the alternate key is the Home key which I never use. So in the AHK script, I just used ControlSend '{HOME]'. I'm satisfied, so even if I knew the correct way to send a ControlClick, I would probably not bother to add it. But then again, I might be curious enough to try it to see if it works and if it worked I might just keep it.

2.) I looked up how to exit loops with another key press, and I saw a few different scripts, some as old as 12 years and one around two years ago. They were all complicated, confusing, and different from each other. Finally, I saw some guy on Reddit say that all he did was use (key)::reload. I tried it out and it worked and I like how simple it is, so I think that I will probably just stick with that.

Below is my actual code.

Code: Select all

#Requires AutoHotkey v2.0

target := 'ahk_exe gamename.exe'


#HotIf WinExist(target)

^+[:: {

Loop {

  Sleep 0500
  ControlSend '{F7}'

  Sleep 0500
  ControlSend '{F8}'

  Sleep 2500
  ControlSend '{Home}'

  Sleep 0025
  ControlSend '{Space}'

  Sleep 0025
  ControlSend '{Space}'

  Sleep 4000
  ControlSend '{F8}'

  Sleep 4000
  ControlSend '{F8}'

  Sleep 4000
  ControlSend '{F8}'

  Sleep 4000
  ControlSend '{F8}'

}}

^+]::reload

#HotIf

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

Re: Could someone please convert my macro into an AHK script that only uses ControlSend commands.

Post by mikeyww » 27 Mar 2023, 13:54

Thank you for sharing the solution. Some additional ideas are below.

Code: Select all

#Requires AutoHotkey v2.0
target := 'ahk_exe gamename.exe'
SoundBeep 2000

^+]::Reload

#HotIf WinExist(target)
^+[:: {
 SoundBeep 1500
 Loop {
  Sleep(  500), ControlSend('{F7}')
  Sleep(  500), ControlSend('{F8}')
  Sleep( 2500), ControlSend('{Home}')
  Loop 2
   Sleep(  25), ControlSend('{Space}')
  Loop 4
   Sleep(4000), ControlSend('{F8}')
 }
}
#HotIf

gaveitatry
Posts: 13
Joined: 12 Feb 2023, 17:37

Re: Could someone please convert my macro into an AHK script that only uses ControlSend commands.

Post by gaveitatry » 27 Mar 2023, 16:42

I updated the script to use a hotkey to end the other hotkey's loop. Before, I was using Refresh, but that had its limitations. I could not add any commands to that hotkey as long as it was using Refresh. I found a simple solution for ending loops that I like and it seems to be working.

Code: Select all

#Requires AutoHotkey v2.0

target := 'ahk_exe gamename.exe'

#HotIf WinExist(target)

^+[:: {

stopTheLoop := False

SoundBeep 1000

Sleep(2000), ControlSend('{F7}')

Loop {

  If (stopTheLoop == True)
  Break

  Sleep( 500), ControlSend('{F10}')
  Sleep(2500), ControlSend('{Home}')
  Loop 2
  Sleep(  25), ControlSend('{Space}')
  Loop 4
  Sleep(4000), ControlSend('{F10}')

}}

^+]:: {

stopTheLoop := True

Sleep( 500), ControlSend('{F8}')

}

#HotIf

gaveitatry
Posts: 13
Joined: 12 Feb 2023, 17:37

Re: Could someone please convert my macro into an AHK script that only uses ControlSend commands.

Post by gaveitatry » 27 Mar 2023, 16:57

Nevermind, I spoke too soon. It doesn't end the loop. I''ll have to keep trying.

gaveitatry
Posts: 13
Joined: 12 Feb 2023, 17:37

Re: Could someone please convert my macro into an AHK script that only uses ControlSend commands.

Post by gaveitatry » 28 Mar 2023, 10:09

The following is the updated script, where the break loop is working.

Code: Select all

#Requires AutoHotkey v2.0

target := 'ahk_exe gamename.exe'

BreakLoop := False

#HotIf WinExist(target)

^+[:: {

global BreakLoop
BreakLoop := False

Sleep(2000), ControlSend('{F7}')

Loop {

  If (BreakLoop = True)
  Break

  Sleep( 500), ControlSend('{F10}')
  Sleep(2500), ControlSend('{Home}')
  Loop 2
  Sleep(  25), ControlSend('{Space}')
  Loop 4
  Sleep(4000), ControlSend('{F10}')

}}

^+]:: {

global BreakLoop
BreakLoop := True

Sleep( 500), ControlSend('{F8}')

}

#HotIf

Post Reply

Return to “Gaming”