AHK To Work IN-GAME Only

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Aluneth
Posts: 14
Joined: 09 Oct 2018, 11:20

AHK To Work IN-GAME Only

09 Oct 2018, 16:40

Hey guys,

Is there a way to make AHK work IN-GAME only?
For example, on my first monitor I want it to spam macros in a game that I play and on the second monitor I want to do other stuff like Photoshop, YouTube, Spotify and so on.

Is it possible to make something like this?
eqv
Posts: 72
Joined: 18 Sep 2018, 22:17

Re: AHK To Work IN-GAME Only

09 Oct 2018, 18:54

You could use the directive "#If" (https://autohotkey.com/docs/commands/_If.htm).
Example:

Code: Select all

#If WinActive("ahk_exe GAME_NAME_HERE.EXE")	;; your game exe here
;; >> Your hotkey\script here <<
#If
Thus; your hotkey\script only works if the game window is active (ps: it can be any win title >> https://autohotkey.com/docs/misc/WinTitle.htm)
·
But, if you wanted send commands (keyboard\click\"orders") directly to the game (without take focus \ active its window); then I recommend this commands:
https://autohotkey.com/docs/commands/ControlSend.htm
https://autohotkey.com/docs/commands/ControlClick.htm
https://autohotkey.com/docs/commands/ControlSetText.htm (don't work very well)
https://autohotkey.com/docs/commands/PostMessage.htm (a bit advanced)

Cheers,
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: AHK To Work IN-GAME Only

09 Oct 2018, 19:05

If you want to send commands to another window than the one that is currently active then you need ControlSend.

This will allow you to work in one window while AHK sends commands to another window.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
Aluneth
Posts: 14
Joined: 09 Oct 2018, 11:20

Re: AHK To Work IN-GAME Only

10 Oct 2018, 12:39

eqv wrote:You could use the directive "#If" (https://autohotkey.com/docs/commands/_If.htm).
Example:

Code: Select all

#If WinActive("ahk_exe GAME_NAME_HERE.EXE")	;; your game exe here
;; >> Your hotkey\script here <<
#If
Thus; your hotkey\script only works if the game window is active (ps: it can be any win title >> https://autohotkey.com/docs/misc/WinTitle.htm)
·
But, if you wanted send commands (keyboard\click\"orders") directly to the game (without take focus \ active its window); then I recommend this commands:
https://autohotkey.com/docs/commands/ControlSend.htm
https://autohotkey.com/docs/commands/ControlClick.htm
https://autohotkey.com/docs/commands/ControlSetText.htm (don't work very well)
https://autohotkey.com/docs/commands/PostMessage.htm (a bit advanced)

Cheers,
FanaticGuru wrote:If you want to send commands to another window than the one that is currently active then you need ControlSend.

This will allow you to work in one window while AHK sends commands to another window.

FG
Thank you guys for answering,
I was messing around with the codes but couldn't understand anything, it is like Japanese to me :|

Currently I am using these macros on my keyboard software in World of Warcraft:
Input Key F2 (spam)
Input Key E (spam)
Right Mouse Button Click (spam)

Could anyone make me a script so I can understand better the basics and mess around with it?
Thanks in advance!
eqv
Posts: 72
Joined: 18 Sep 2018, 22:17

Re: AHK To Work IN-GAME Only

10 Oct 2018, 20:18

Here an example of the «directive» "#If" using notepad:

Code: Select all

;; Below hotkeys will only work IN notepad (note "#If WinActive..."),
#If WinActive("ahk_exe notepad.exe")
F2::
Msgbox, You press "F2" in Notepad.
Return

E::
Msgbox, You press "E" in Notepad.
Return

RButton::
Msgbox, You press "Right Click" in Notepad.
Return
#If

;; Below hotkeys will only work OUTSIDE notepad (note last "#If" close directive),
F2::
Msgbox, You press "F2" OUTSIDE of Notepad.
Return

E::
Msgbox, You press "E" OUTSIDE of Notepad.
Return

RButton::
Msgbox, You press "Right Click" OUTSIDE of Notepad.
Return

• Note that it only works when you are using notepad.
• Just replaces "notepad.exe", by the World of Warcraft exe (doing a fast view on the internet, I think it is "WoW.exe" or "WoW-64.exe")
·
·
Here an example using notepad again

Code: Select all

;; ControlSend.  send keystroke (includ "{tab}","{space}") to the window control,
;; In this example, it send "Hello World!" plus an [enter], and "...in notepad",
F2::
ControlSend, Edit1, Hello World!{enter}...in notepad, ahk_exe notepad.exe
Return

;; ControlClick.  send clicks to the window control (or position),
;; In this example, it send right click to the notepad window,
E::
ControlClick, Edit1, ahk_exe notepad.exe,, RIGHT,, NA
Return
You need to learn these concepts in order to apply these commands:
• Win Title
• WinSpy (from Autohotkey folder)
• Mouse Position (absolute, relative, client)
• ClassNN (or Control) {this just for "control" commands}

If you have some problem just let us know.
[note: "postmessage" directly gives the order to the window (no simulate keystrokes or clicks), but I actually don't know very well how to use it :| ].
Cheers,
Aluneth
Posts: 14
Joined: 09 Oct 2018, 11:20

Re: AHK To Work IN-GAME Only

11 Oct 2018, 02:03

eqv wrote:Here an example of the «directive» "#If" using notepad:

Code: Select all

;; Below hotkeys will only work IN notepad (note "#If WinActive..."),
#If WinActive("ahk_exe notepad.exe")
F2::
Msgbox, You press "F2" in Notepad.
Return

E::
Msgbox, You press "E" in Notepad.
Return

RButton::
Msgbox, You press "Right Click" in Notepad.
Return
#If

;; Below hotkeys will only work OUTSIDE notepad (note last "#If" close directive),
F2::
Msgbox, You press "F2" OUTSIDE of Notepad.
Return

E::
Msgbox, You press "E" OUTSIDE of Notepad.
Return

RButton::
Msgbox, You press "Right Click" OUTSIDE of Notepad.
Return

• Note that it only works when you are using notepad.
• Just replaces "notepad.exe", by the World of Warcraft exe (doing a fast view on the internet, I think it is "WoW.exe" or "WoW-64.exe")
·
·
Here an example using notepad again

Code: Select all

;; ControlSend.  send keystroke (includ "{tab}","{space}") to the window control,
;; In this example, it send "Hello World!" plus an [enter], and "...in notepad",
F2::
ControlSend, Edit1, Hello World!{enter}...in notepad, ahk_exe notepad.exe
Return

;; ControlClick.  send clicks to the window control (or position),
;; In this example, it send right click to the notepad window,
E::
ControlClick, Edit1, ahk_exe notepad.exe,, RIGHT,, NA
Return
You need to learn these concepts in order to apply these commands:
• Win Title
• WinSpy (from Autohotkey folder)
• Mouse Position (absolute, relative, client)
• ClassNN (or Control) {this just for "control" commands}

If you have some problem just let us know.
[note: "postmessage" directly gives the order to the window (no simulate keystrokes or clicks), but I actually don't know very well how to use it :| ].
Cheers,
Thank you so much!

Can the script also work when World of Warcraft is minimized?
Also, the script should work as it is on one monitor and I will be able to use my second monitor without getting interrupted by the button pressing right?

Thanks again!
eqv
Posts: 72
Joined: 18 Sep 2018, 22:17

Re: AHK To Work IN-GAME Only

11 Oct 2018, 02:40

I actually don't have multiple monitor, sorry; but in theory it should work with no problem.
·
The "Control" commands (2° example); works on minimized windows (even hide windows), and in the "background" (don't affect the current window, nor does it remove the focus from the current window).
·
I highly recommend reading the related documentation, you could discover very useful functionalities:
https://autohotkey.com/docs/commands/ControlSend.htm

Cheers,
Aluneth
Posts: 14
Joined: 09 Oct 2018, 11:20

Re: AHK To Work IN-GAME Only

11 Oct 2018, 03:23

eqv wrote:I actually don't have multiple monitor, sorry; but in theory it should work with no problem.
·
The "Control" commands (2° example); works on minimized windows (even hide windows), and in the "background" (don't affect the current window, nor does it remove the focus from the current window).
·
I highly recommend reading the related documentation, you could discover very useful functionalities:
https://autohotkey.com/docs/commands/ControlSend.htm

Cheers,
Thanks again for replying,
so I tried this script now and it doesn't seems to work, and when I do click on "E"/"L Mouse Button Click" for example the buttons just does not function.
Can you see what's wrong with it?
I tested this while World of Warcraft is running.

Code: Select all

;; Below hotkeys will only work IN notepad (note "#If WinActive..."),
#If WinActive("ahk_exe Wow.exe")
E::
Return

LButton::
Return
#If
Thanks!
eqv
Posts: 72
Joined: 18 Sep 2018, 22:17

Re: AHK To Work IN-GAME Only

11 Oct 2018, 03:43

I don't get it, you have programmed "E" and "LButton" to do nothing when "Wow.exe" is active.
So, if you press while focusing the game; it won't do anything but outside of it should work normally.
·
"#If" DON'T send the key "E" or "LButton" to the game; for that you need "ControlSend" or "ControlClick".

I think is better if you posted you script, and I try to adapt it.
Aluneth
Posts: 14
Joined: 09 Oct 2018, 11:20

Re: AHK To Work IN-GAME Only

11 Oct 2018, 04:02

eqv wrote:I don't get it, you have programmed "E" and "LButton" to do nothing when "Wow.exe" is active.
So, if you press while focusing the game; it won't do anything but outside of it should work normally.
·
"#If" DON'T send the key "E" or "LButton" to the game; for that you need "ControlSend" or "ControlClick".

I think is better if you posted you script, and I try to adapt it.
I'm confused,

This is my script :lol:

Code: Select all

;; Below hotkeys will only work IN notepad (note "#If WinActive..."),
#If WinActive("ahk_exe Wow.exe")
E::
Return

LButton::
Return
#If
I thought that the script that you've sent me is the script that I should use in order to make it work, my bad I guess.
I just can't understand how to make it work.

I want it to spam the button "E" in WoW.exe
Spam the "Lbutton"
Then again "E" then "Lbutton"
I want it to run in a loop basically.

Thanks.
eqv
Posts: 72
Joined: 18 Sep 2018, 22:17

Re: AHK To Work IN-GAME Only

11 Oct 2018, 04:17

Oooh, okay now I get it. Let's do multiple test (step-by-step).
Then you need to know control; but let's try this first:

Code: Select all

F1::
ControlSend,, E, ahk_exe Wow.exe	;; This should always send "E" to wow, each time you press F1
Return
If that work, now let's try plus "LButton" (if there is an keyboard alternative like ENTER would be better):

Code: Select all

F1::
ControlSend,, E, ahk_exe Wow.exe	;; This should always send "E" and "LButton" to wow.
Sleep, 100							;; little delay
ControlClick,, ahk_exe Wow.exe,, Left,, NA
Return
If that works, let's add the loop (if you want, add a hotkey to active it):

Code: Select all

Loop
{
ControlSend,, E, ahk_exe Wow.exe
Sleep, 100
ControlClick,, ahk_exe Wow.exe,, Left,, NA
Sleep, 100
}
Return
If the "LButton" part doesn't work, and there is no keyboard alternative (like ENTER or SPACE); then you need to know its "control" in the game. The problem is in games, I think there isn't any; so you need to press the position on the screen.

Let me know, how you go with the scripts.
Regards,
Aluneth
Posts: 14
Joined: 09 Oct 2018, 11:20

Re: AHK To Work IN-GAME Only

11 Oct 2018, 05:38

eqv wrote:Oooh, okay now I get it. Let's do multiple test (step-by-step).
Then you need to know control; but let's try this first:

Code: Select all

F1::
ControlSend,, E, ahk_exe Wow.exe	;; This should always send "E" to wow, each time you press F1
Return
If that work, now let's try plus "LButton" (if there is an keyboard alternative like ENTER would be better):

Code: Select all

F1::
ControlSend,, E, ahk_exe Wow.exe	;; This should always send "E" and "LButton" to wow.
Sleep, 100							;; little delay
ControlClick,, ahk_exe Wow.exe,, Left,, NA
Return
If that works, let's add the loop (if you want, add a hotkey to active it):

Code: Select all

Loop
{
ControlSend,, E, ahk_exe Wow.exe
Sleep, 100
ControlClick,, ahk_exe Wow.exe,, Left,, NA
Sleep, 100
}
Return
If the "LButton" part doesn't work, and there is no keyboard alternative (like ENTER or SPACE); then you need to know its "control" in the game. The problem is in games, I think there isn't any; so you need to press the position on the screen.

Let me know, how you go with the scripts.
Regards,
Okay I've just tested it and it works besides the mouse button click,
I have to use a mouse button since it points to a specific direction.

Is there a code that is used for X and Y on the screen or something similar?
So maybe if we put a direction to it, it will know where to use that mouse click on?
eqv
Posts: 72
Joined: 18 Sep 2018, 22:17

Re: AHK To Work IN-GAME Only

11 Oct 2018, 06:14

Yes, use this hotkey to get the relative position in the game of what you want to press (IMPORTANT: first activate\focus WoW, then run the hotkey):

Code: Select all

F1::
Coordmode, Mouse, Relative
MouseGetPos, X, Y
Msgbox % "x"X . "`n" . "y"Y
Return
Record the coordinates and replace them in the command:

Code: Select all

F1::
SetControlDelay -1
ControlSend,, E, ahk_exe Wow.exe
Sleep, 100
ControlClick, x000 y000, ahk_exe Wow.exe,, Left,, NA	;; <<< REPLACE HERE ("x000 y000")
Sleep, 100
Return
If it works, then just add the loop.

Cheers,
Aluneth
Posts: 14
Joined: 09 Oct 2018, 11:20

Re: AHK To Work IN-GAME Only

11 Oct 2018, 07:23

eqv wrote:Yes, use this hotkey to get the relative position in the game of what you want to press (IMPORTANT: first activate\focus WoW, then run the hotkey):

Code: Select all

F1::
Coordmode, Mouse, Relative
MouseGetPos, X, Y
Msgbox % "x"X . "`n" . "y"Y
Return
Record the coordinates and replace them in the command:

Code: Select all

F1::
SetControlDelay -1
ControlSend,, E, ahk_exe Wow.exe
Sleep, 100
ControlClick, x000 y000, ahk_exe Wow.exe,, Left,, NA	;; <<< REPLACE HERE ("x000 y000")
Sleep, 100
Return
If it works, then just add the loop.

Cheers,
Hey so I got the "X" and "Y" of the mouse position in-game and it works,
but for some reason when the game is minimized it will continue moving my mouse to the same position.

Maybe the script won't recognize the in-game window? and does recognize the screen's "x" and "y" and that must be the problem?
How do I get the in-game mouse coordinates then?
Also I would like to know what to add so I can stop the screen let's say for example on button "`"
Here is the progress of it,

Code: Select all

#If WinActive("ahk_exe Wow.exe")
F2::
Loop
{
ControlSend,, 1, ahk_exe Wow.exe
Sleep, 100
ControlClick, x1263 y560, ahk_exe Wow.exe,, Left,, NA
Sleep, 100
ControlSend,, 2, ahk_exe Wow.exe
Sleep, 100
}
Return
#If
Last edited by Aluneth on 11 Oct 2018, 07:58, edited 1 time in total.
eqv
Posts: 72
Joined: 18 Sep 2018, 22:17

Re: AHK To Work IN-GAME Only

11 Oct 2018, 07:54

Hello,
• Actually "ControlClick" shouldn't move your mouse, just simulate the clicks in the position.
• If you want stop the loop, then you can use a variable for it. I change the "loop" for a "while", is the same, but "while" can have conditions.
• In theory you could use any character as hotkey, but some have problems and depend on the keyboard.
So in the example below, I turn F2 into a toggle button, press start the loop, press again stop the loop.
• More information about keys and hotkeys (check special keys at the end):
https://autohotkey.com/docs/KeyList.htm

Code: Select all

#MaxThreadsPerHotkey 2
Coordmode, Mouse, Relative
toggle:=false
#If WinActive("ahk_exe Wow.exe")
F2::
toggle:=!toggle		;; << variable for stop loop. (tip: "!" means "no" or "negative")
While (toggle) {	;; << condition of loop.
ControlSend,, 1, ahk_exe Wow.exe
Sleep, 100
ControlClick, x1263 y560, ahk_exe Wow.exe,, Left,, NA
Sleep, 100
ControlSend,, 2, ahk_exe Wow.exe
Sleep, 100
}
Return
#If
About the problem of the position, I have no experience using scripts in games but I have heard that they are problematic (the game is fighting to take the key and also anti-cheat systems).

The script works well if maximize, and focusing somewhere else? If so, we could try to "hide" the game window, but keep its size.
Cheers,
Aluneth
Posts: 14
Joined: 09 Oct 2018, 11:20

Re: AHK To Work IN-GAME Only

11 Oct 2018, 08:10

eqv wrote:Hello,
• Actually "ControlClick" shouldn't move your mouse, just simulate the clicks in the position.
• If you want stop the loop, then you can use a variable for it. I change the "loop" for a "while", is the same, but "while" can have conditions.
• In theory you could use any character as hotkey, but some have problems and depend on the keyboard.
So in the example below, I turn F2 into a toggle button, press start the loop, press again stop the loop.
• More information about keys and hotkeys (check special keys at the end):
https://autohotkey.com/docs/KeyList.htm

Code: Select all

#MaxThreadsPerHotkey 2
Coordmode, Mouse, Relative
toggle:=false
#If WinActive("ahk_exe Wow.exe")
F2::
toggle:=!toggle		;; << variable for stop loop. (tip: "!" means "no" or "negative")
While (toggle) {	;; << condition of loop.
ControlSend,, 1, ahk_exe Wow.exe
Sleep, 100
ControlClick, x1263 y560, ahk_exe Wow.exe,, Left,, NA
Sleep, 100
ControlSend,, 2, ahk_exe Wow.exe
Sleep, 100
}
Return
#If
About the problem of the position, I have no experience using scripts in games but I have heard that they are problematic (the game is fighting to take the key and also anti-cheat systems).

The script works well if maximize, and focusing somewhere else? If so, we could try to "hide" the game window, but keep its size.
Cheers,
Okay I try to explain myself better :oops:
When in-game I CAN move my corsair anywhere I want,
When game is minimized - my corsair is going back to a position that I coordinated to my X and Y.
Therefor I can't use my second monitor cause my corsair always going back to the position I coordinated. (where WoW is launched, 1st monitor)
eqv
Posts: 72
Joined: 18 Sep 2018, 22:17

Re: AHK To Work IN-GAME Only

11 Oct 2018, 10:47

Try adding this at the over "ControlClick" as specified in the documentation:
(https://autohotkey.com/docs/commands/ControlClick.htm)
SetControlDelay -1

I am sorry; I don't have multiple monitors, nor the game to test it :( .
Keep looking for "ControlClick" and multiple monitors, I guess the problem must be there.

Good Luck :thumbup:
Aluneth
Posts: 14
Joined: 09 Oct 2018, 11:20

Re: AHK To Work IN-GAME Only

11 Oct 2018, 11:06

eqv wrote:Try adding this at the over "ControlClick" as specified in the documentation:
(https://autohotkey.com/docs/commands/ControlClick.htm)
SetControlDelay -1

I am sorry; I don't have multiple monitors, nor the game to test it :( .
Keep looking for "ControlClick" and multiple monitors, I guess the problem must be there.

Good Luck :thumbup:
Okay so this is my code now:

Code: Select all

#MaxThreadsPerHotkey 2
Coordmode, Mouse, Relative
toggle:=false
#If WinActive("ahk_exe Wow.exe")
F2::
toggle:=!toggle		;; << variable for stop loop. (tip: "!" means "no" or "negative")
While (toggle) {	;; << condition of loop.
ControlSend,, 1, ahk_exe Wow.exe
Sleep, 100
SetControlDelay -1
ControlClick, x1263 y560, ahk_exe Wow.exe,, Left,, NA
Sleep, 100
ControlSend,, 2, ahk_exe Wow.exe
Sleep, 100
}
Return
#If
I've tested it and it didn't work, my mouse corsair when in game I can move it around and mess with it but when I'm outside of the game it goes to the "X" and "Y" I coordinated :eh:
Does the script looks good to you and should work?
Ruevil2
Posts: 173
Joined: 14 Jul 2014, 10:39

Re: AHK To Work IN-GAME Only

11 Oct 2018, 14:22

Aluneth wrote: Okay so this is my code now:

Code: Select all

#MaxThreadsPerHotkey 2
Coordmode, Mouse, Relative
toggle:=false
#If WinActive("ahk_exe Wow.exe")
F2::
toggle:=!toggle		;; << variable for stop loop. (tip: "!" means "no" or "negative")
While (toggle) {	;; << condition of loop.
ControlSend,, 1, ahk_exe Wow.exe
Sleep, 100
SetControlDelay -1
ControlClick, x1263 y560, ahk_exe Wow.exe, , Left, , NA D
Sleep, 100
ControlSend,, 2, ahk_exe Wow.exe
Sleep, 100
}
Return
#If
I've tested it and it didn't work, my mouse corsair when in game I can move it around and mess with it but when I'm outside of the game it goes to the "X" and "Y" I coordinated :eh:
Does the script looks good to you and should work?
It sounds like you are trying to make a bot that will do some actions in-game for you while you use the mouse to complete other tasks on a second monitor. Is this correct?

Edit:
I'm going to assume this is what you mean. A couple problems with this code.

#1: #IfWinActive is the totally wrong thing to use here. The 'active' window is the one that is currently focused. So this will not work on minimized or unfocused windows and will not allow you to use other programs while the bot is running.


#2: ControlClick is very unreliable in games. 90% of games don't use any controls whatsoever. The only upside to using Controlclick is that it has minimal 'mouse takeover'. I suggest adding individual up/down commands with a sleep in between although this may present a problem with your mouse usage on other programs. (See #4 for explanation of why a 'hold down pause' is necessary in most games).

With ControlSend

Code: Select all

ControlClick, x1263 y560, ahk_exe Wow.exe, , Left, , NA D
Sleep, 50
ControlClick, x1263 y560, ahk_exe Wow.exe, , Left, , NA U

#3: SetControlDelay -1, is also a mistake(for most games).
"This avoids holding the mouse button down during the click, which in turn reduces interference from the user's physical movement of the mouse."
This will directly cause problems with your hotkeys being 'read by the game. It has to do with DirectX polling methods for capturing keyboard data and
will cause scripts to 'skip' key presses at random.

See #3 in this post for explanation of 'hold-down pause' and why it is necessary.
https://autohotkey.com/boards/viewtopic ... 7&t=11084

#4: If you have to read pixel colors, you CANNOT run the game minimized. Pixel/image searches are based off of the current screen buffer and does not detect minimized windows at all. There is no way around this as the only data in memory is the current pixel display.


Final Thoughts: Unfortunately, you will probably be banned for this. I know for a fact that Blizzard is detecting AHK bots as far back as 10 years ago, I got warnings when I use to play and bot, I quit during WotLK. If you want to try anyway you need to add some 'humanizing' factors to your code. Vary the pixel location clicked by a small amount, vary sleep times by a small amount, don't click faster or move the mouse faster than a human could, have your bot take small random breaks(like a person would with bio or food).
eqv
Posts: 72
Joined: 18 Sep 2018, 22:17

Re: AHK To Work IN-GAME Only

11 Oct 2018, 19:03

A small correction:
• #1: "#If WinActive()", only affect the thread (hotkey) «execution». Once is running it won't affect the thread (notice the loop \ while). So it would go:
* Activate(Wow) → Start(Hotkey\Thread) -- {do stuff outside} -- * Activate. Wow → End(Hotkey\Thread)

On the rest "Ruevil" it's right, It must be a problem with the game. Someone who had the game could advise you better how to make it work (additionally so that you do not get banned).

Cheers,

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, Google [Bot] and 252 guests