Help with sending keys to a game. Topic is solved

Ask gaming related questions (AHK v1.1 and older)
jayblah
Posts: 13
Joined: 17 Oct 2016, 10:20

Help with sending keys to a game.

Post by jayblah » 14 May 2022, 21:53

#IfWinActive ahk_exe gameprocess.exe

Code: Select all

F1::
    Toggle := !Toggle
    While Toggle {
        Send !Q
        Sleep 100
        Send R
        Sleep 100
        Send D
        Sleep 100
    }
Return
Hello, I need help with the above script. The issues are as follows:

1. The F1 key is not consistently stopping the key spam.

2. The script spams even outside the specified process. I used WindowSpy to identify the correct process name.

3. The script doesn't spam the keys if I'm interacting with the process (a game) through mouseclicks. Can I get it to work despite me also clicking?

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

Re: Help with sending keys to a game.

Post by mikeyww » 14 May 2022, 22:01

Welcome to this AutoHotkey forum!

Use lowercase letters. Add #MaxThreadsPerHotkey 2 to the top. If you and AHK are both acting at the same time, your actions could collide.

Code: Select all

winTitle = ahk_exe gameprocess.exe
#MaxThreadsPerHotkey 2
#If WinActive(winTitle)
F1::
on := !on
SetKeyDelay, 110
While on
 Loop, Parse, % "qrd"
  Send % on ? A_LoopField : ""
Return
#If
The active window is checked only when the hotkey is triggered. If you subsequently change the active window, the keys will be sent there.

Test in Notepad.

jayblah
Posts: 13
Joined: 17 Oct 2016, 10:20

Re: Help with sending keys to a game.

Post by jayblah » 14 May 2022, 22:43

mikeyww wrote:
14 May 2022, 22:01
Welcome to this AutoHotkey forum!

Use lowercase letters. Add #MaxThreadsPerHotkey 2 to the top. If you and AHK are both acting at the same time, your actions could collide.

Code: Select all

winTitle = ahk_exe gameprocess.exe
#MaxThreadsPerHotkey 2
#If WinActive(winTitle)
F1::
on := !on
SetKeyDelay, 110
While on
 Loop, Parse, % "qrd"
  Send % on ? A_LoopField : ""
Return
#If
The active window is checked only when the hotkey is triggered. If you subsequently change the active window, the keys will be sent there.

Test in Notepad.
Thanks for the response. Most of that seems to work, except I am trying to sent LAlt+Q instead of just Q (LAlt+Q, R, D). How can I send LAlt+Q in your "Loop, Parse, %"qrd" syntax?

Would it be "Loop, Parse %!qrd"?

jayblah
Posts: 13
Joined: 17 Oct 2016, 10:20

Re: Help with sending keys to a game.

Post by jayblah » 14 May 2022, 22:49

I'd like to send LAlt+Q instead of just Q in this code: " Loop, Parse, % "qrd"

How do I do that? I assume Loop, Parse, % "!qrd" will not work? Also, what exactly is the line Send % on ? A_LoopField : "" doing?

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

Re: Help with sending keys to a game.

Post by mikeyww » 15 May 2022, 05:04

OK. An update is below. The ternary operator acts like "If... then... else...", so the key is sent only if on is set.

Code: Select all

winTitle = ahk_exe gameprocess.exe
#MaxThreadsPerHotkey 2
#If WinActive(winTitle)
F1::
on := !on
SetKeyDelay, 110
While on
 For each, key in ["!q", "r", "d"]
  Send % on ? key : ""
Return
#If

jayblah
Posts: 13
Joined: 17 Oct 2016, 10:20

Re: Help with sending keys to a game.

Post by jayblah » 15 May 2022, 14:54

Thank you for your help, that worked.

jayblah
Posts: 13
Joined: 17 Oct 2016, 10:20

Re: Help with sending keys to a game.

Post by jayblah » 19 May 2022, 16:02

mikeyww wrote:
15 May 2022, 05:04
OK. An update is below. The ternary operator acts like "If... then... else...", so the key is sent only if on is set.

Code: Select all

winTitle = ahk_exe gameprocess.exe
#MaxThreadsPerHotkey 2
#If WinActive(winTitle)
F1::
on := !on
SetKeyDelay, 110
While on
 For each, key in ["!q", "r", "d"]
  Send % on ? key : ""
Return
#If
Can you teach me how I can separate the "R" key out of that statement and into a separate loop? What I'd like it to do is that when I press "R", it loops through clicking "R". If I press "R" again, it stops. If that can't be done, I'd be fine with an "Alt + R" keypress as the toggle.

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

Re: Help with sending keys to a game.

Post by mikeyww » 19 May 2022, 16:14

By example... :)

Code: Select all

winTitle = ahk_exe gameprocess.exe

#MaxThreadsPerHotkey 2

#If WinActive(winTitle)

r::SetTimer, r Up, % (r := !r) ? 110 : "Off"
r Up::Send % r ? "r" : ""

F1::
on := !on
SetKeyDelay, 110
While on
 For each, key in ["!q", "d"]
  Send % on ? key : ""
Return

#If

jayblah
Posts: 13
Joined: 17 Oct 2016, 10:20

Re: Help with sending keys to a game.

Post by jayblah » 19 May 2022, 19:27

You're awesome.

So, using your example, I just did the following:

Code: Select all

winTitle = ahk_exe HeroesOfTheStorm_x64.exe

#MaxThreadsPerHotkey 2

#If WinActive(winTitle)

q::SetTimer, q Up, % (q := !q) ? 500 : "Off"
q Up::Send % q ? "!q" : ""

w::SetTimer, w Up, % (w := !w) ? 500 : "Off"
w Up::Send % w ? "w" : ""

e::SetTimer, e Up, % (e := !e) ? 500 : "Off"
e Up::Send % e ? "e" : ""

r::SetTimer, r Up, % (r := !r) ? 500 : "Off"
r Up::Send % r ? "r" : ""

d::SetTimer, d Up, % (d := !d) ? 500 : "Off"
d Up::Send % d ? "d" : ""

z::SetTimer, z Up, % (z := !z) ? 500 : "Off"
z Up::Send % z ? "z" : ""
This script toggles each button (Q, W, E, R, D, Z) on or off so that they click repeatedly. Is there a better way to do this?

Also, is there a way to assign a separate button (e.g. F1) that will STOP all buttons that are toggled ON and set them to OFF?

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

Re: Help with sending keys to a game.  Topic is solved

Post by mikeyww » 19 May 2022, 20:09

There are various ways but not necessarily better.

Code: Select all

F1::
Loop, Parse, % "qwerdz"
{ %A_LoopField% := False
  SetTimer, %A_LoopField% Up, Off
}
SoundBeep, 1000
Return

jayblah
Posts: 13
Joined: 17 Oct 2016, 10:20

Re: Help with sending keys to a game.

Post by jayblah » 20 May 2022, 09:51

mikeyww wrote:
19 May 2022, 20:09
There are various ways but not necessarily better.

Code: Select all

F1::
Loop, Parse, % "qwerdz"
{ %A_LoopField% := False
  SetTimer, %A_LoopField% Up, Off
}
SoundBeep, 1000
Return
That worked. Thank you. I tried using "z" as the global on/off toggle instead of F1, and that didn't appear to work. I suspect that's because "z" is in use as a key for the looping portion as well.

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

Re: Help with sending keys to a game.

Post by mikeyww » 20 May 2022, 11:59

Yep. You can use that letter, but if you want that letter (hotkey) to do multiple things depending on other variables or conditions, then you would need to write a hotkey routine for it that checked and did those things. For any given context (e.g., #If), you can define a specific hotkey only once.

jayblah
Posts: 13
Joined: 17 Oct 2016, 10:20

Re: Help with sending keys to a game.

Post by jayblah » 22 May 2022, 15:31

Heya Mikey,


Can you teach me how to improve this script as follows?

Code: Select all

winTitle = ahk_exe game.exe

#MaxThreadsPerHotkey 2

#If WinActive(winTitle)

q::SetTimer, q Up, % (q := !q) ? 250 : "Off"
q Up::Send % q ? "!q" : ""

;w::SetTimer, w Up, % (w := !w) ? 250 : "Off"
;w Up::Send % w ? "!w" : ""

;e::SetTimer, e Up, % (e := !e) ? 250 : "Off"
;e Up::Send % e ? "e" : ""

r::SetTimer, r Up, % (r := !r) ? 250 : "Off"
r Up::Send % r ? "r" : ""

d::SetTimer, d Up, % (d := !d) ? 250 : "Off"
d Up::Send % d ? "d" : ""

F1::
Loop, Parse, % "qrd"
{ %A_LoopField% := False
  SetTimer, %A_LoopField% Up, Off
}
SoundBeep, 1000
Return
Issue 1:
Instead of "F1", I'd actually like "Z" to be the killswitch to disable all the loops. However, I've found that if I use "Z" to be the killswitch, I can't actually use Z in-game to do what Z is supposed to be doing.

Issue 2:
For the line:

Code: Select all

r::SetTimer, r Up, % (r := !r) ? 250 : "Off"
r Up::Send % r ? "r" : ""
Rather than loop indefinitely until "R" is pressed to turn it off, what I'd like this to be doing is: When I press "R", the loop starts (spams the "R" keypress). After 20 seconds, the loop stops (no more "R" keypress spam), requiring me to press "R" again to re-start the loop. How would this be achieved?

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

Re: Help with sending keys to a game.

Post by mikeyww » 22 May 2022, 16:40

Some ideas for you here:

Code: Select all

$r::
SoundBeep, 1500
SetTimer, R, 250
SetTimer, RStop, -20000
R:
Send r
Return

RStop:
SetTimer,, Off
SetTimer, R, Off
SoundBeep, 1000
Return

~z::
Loop, Parse, % "R"
 Gosub, %A_LoopField%Stop
Return
Explained: TildeDollar

jayblah
Posts: 13
Joined: 17 Oct 2016, 10:20

Re: Help with sending keys to a game.

Post by jayblah » 22 May 2022, 19:24

When I try:

Code: Select all

~z::
Loop, Parse, % "qrd"
 Gosub, %A_LoopField%Stop
Return
(Note: I added "q" and "d" because I want Q, R, and D to stop being sent.)

When I added the above to the code:

Code: Select all

winTitle = ahk_exe HeroesOfTheStorm_x64.exe

#MaxThreadsPerHotkey 2

#If WinActive(winTitle)

q::SetTimer, q Up, % (q := !q) ? 250 : "Off"
q Up::Send % q ? "!q" : ""

d::SetTimer, d Up, % (d := !d) ? 250 : "Off"
d Up::Send % d ? "d" : ""

$r::
SoundBeep, 1500
SetTimer, R, 250
SetTimer, RStop, -20000
R:
Send r
Return

RStop:
SetTimer,, Off
SetTimer, R, Off
SoundBeep, 1000
Return

~z::
Loop, Parse, % "qrd"
 Gosub, %A_LoopField%Stop
Return
I get an error: Target label does not exist.

Specifically: qStop
Line #046: Gosub, %A_LoopField%Stop

Not sure what's going wrong.

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

Re: Help with sending keys to a game.

Post by mikeyww » 22 May 2022, 19:33

I was providing a demonstration for how you could change the routines according to your newly posted needs. You would change the other routines similarly, if that is what you want.

jayblah
Posts: 13
Joined: 17 Oct 2016, 10:20

Re: Help with sending keys to a game.

Post by jayblah » 22 May 2022, 20:55

Hrm. Still not having any success getting it to work. Consider this:

Code: Select all

q::SetTimer, q Up, % (q := !q) ? 250 : "Off"
q Up::Send % q ? "!q" : ""
SoundBeep, 500

~z::
Loop, Parse, % "q Up"
Gosub, %A_LoopField%Stop
SoundBeep, 1000
Return
Problem: I get an error here. It says:
Error: Target label does not exist.
Specifically: qStop
Line 021: Gosub, %A_LoopField%Stop
Not really sure what I put in Line 21 to get it to recognize the "q" loop.

jayblah
Posts: 13
Joined: 17 Oct 2016, 10:20

Re: Help with sending keys to a game.

Post by jayblah » 22 May 2022, 21:02

I think I got it now, Mikey:

Code: Select all

#MaxThreadsPerHotkey 2

#If WinActive(winTitle)

$q::
SoundBeep, 500
SetTimer, Q, 250

Q:
Send q
Return

QStop:
SetTimer,, Off
SetTimer, Q, Off
SoundBeep, 1000
Return

~z::
Loop, Parse, % "q"
Gosub, %A_LoopField%Stop
SoundBeep, 1000
Return

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

Re: Help with sending keys to a game.

Post by mikeyww » 23 May 2022, 03:39

Good to hear. You can set a timer for QStop wherever you want that to occur.

jayblah
Posts: 13
Joined: 17 Oct 2016, 10:20

Re: Help with sending keys to a game.

Post by jayblah » 24 May 2022, 11:15

Hey, I did some searching and found a thread to which you had responded to for captions. Based on that, I've come up with this:

Code: Select all

winTitle = ahk_exe game.exe

#MaxThreadsPerHotkey 2

#If WinActive(winTitle)

$q::
SoundBeep, 1000, 250
SetTimer, Q, 250
text = Spam ON
Gui, -Caption +AlwaysOnTop
Gui, Font, s14
Gui, Add, Text,, %text%
Gui, Show, x5 y20

Q:
Send q
Return

QStop:
SetTimer,, Off
SetTimer, Q, Off
SoundBeep, 1000
Gui, Hide
Return

~z::
Loop, Parse, % "q"
Gosub, %A_LoopField%Stop
SoundBeep, 250, 250
Gui, Hide
Return
I'm seeing several issues:
1. When I press "Q" the first time, the script spams several Windows error sound. Unsure why.
2. If I press "Q" a second time, the Q key does not stop spamming. Also, Windows goes haywire with the error sound. It also does not hide the GUI element, even though that's part of the function. Almost seems like the QStop function isn't working at all.
3. Pressing "Z" stops the script properly and hides the GUI element as well.

Would you be able to assist with solving issues 1 and 2?

Thank you.

Post Reply

Return to “Gaming Help (v1)”