Running a script on a specific window even while alt tabbed.

Ask gaming related questions (AHK v1.1 and older)
2kmentality
Posts: 16
Joined: 21 Apr 2021, 15:21

Running a script on a specific window even while alt tabbed.

Post by 2kmentality » 23 Apr 2021, 07:38

Hey guys. So I have this script that runs on a game to afk farm. I noticed that the script still runs even when the game window is in the background, hence messing things up on my desktop.

Is there a way to run the script only on the game window? Even while its on the background or alt-tabbed? I want to do other tasks on my PC while doing it so any help will be much appreciated.

Greetings from the Philippines. :wave:
User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: Running a script on a specific window even while alt tabbed.

Post by mikeyww » 23 Apr 2021, 12:15

You can change Send to ControlSend. It does not respond to all windows. Post your script for feedback.
2kmentality
Posts: 16
Joined: 21 Apr 2021, 15:21

Re: Running a script on a specific window even while alt tabbed.

Post by 2kmentality » 23 Apr 2021, 18:17

Here's the code I'm using.

Code: Select all

#MaxThreadsperHotkey 2
NumpadAdd::ExitApp

~F1::
Find := !Find
While Find
{
PixelSearch, Px, Py, 662, 662, 60, 60, 0x618EB5, 1, Fast
If ErrorLevel
{
Sleep, 400
MouseMove 405, 325
Send, {F1}
Sleep, 150
MouseClick, Left
Sleep, 400
Send, {F3}
}
Else
{
Sleep, 2000
Send, @warp prontera 162 170{Enter}
Sleep, 2000
MouseMove 495, 290
Sleep, 2000
MouseClick, Left
Sleep, 2000
Send, {Enter}
Sleep, 2000
}
}
Return
2kmentality
Posts: 16
Joined: 21 Apr 2021, 15:21

Re: Running a script on a specific window even while alt tabbed.

Post by 2kmentality » 23 Apr 2021, 18:50

Untitled.png
Untitled.png (18.23 KiB) Viewed 5066 times
User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: Running a script on a specific window even while alt tabbed.

Post by mikeyww » 23 Apr 2021, 19:09

Example:

Code: Select all

ControlSend,, {F1}, Mob Ragnarok
In some cases, tips for games can also be helpful.

https://autohotkey.com/board/topic/111737-how-to-make-ahk-work-in-most-games-the-basics/
2kmentality
Posts: 16
Joined: 21 Apr 2021, 15:21

Re: Running a script on a specific window even while alt tabbed.

Post by 2kmentality » 23 Apr 2021, 20:02

mikeyww wrote:
23 Apr 2021, 19:09
Example:

Code: Select all

ControlSend,, {F1}, Mob Ragnarok
In some cases, tips for games can also be helpful.

https://autohotkey.com/board/topic/111737-how-to-make-ahk-work-in-most-games-the-basics/
Should I just place this at the top of my code?
2kmentality
Posts: 16
Joined: 21 Apr 2021, 15:21

Re: Running a script on a specific window even while alt tabbed.

Post by 2kmentality » 23 Apr 2021, 20:09

Code: Select all

NumPadAdd::ExitApp
#MaxThreadsperHotkey 2

~F1::
Previouskey:= !PreviousKey
Loop
{
If PreviousKey
{
Sleep, 200
MouseMove 405, 325
Send, {F1}
Sleep, 150
MouseClick, Left
Sleep, 200
Send, {F3}
}
Else
Break
}
Return
Given this code for example, how should I integrate it?
User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: Running a script on a specific window even while alt tabbed.

Post by mikeyww » 23 Apr 2021, 20:37

It's an example instead of Send {F1}. It shows you how you can use ControlSend as a command, to send to an inactive window. Instead of trying your entire script, try just one ControlSend command, to see if it works.
2kmentality
Posts: 16
Joined: 21 Apr 2021, 15:21

Re: Running a script on a specific window even while alt tabbed.

Post by 2kmentality » 23 Apr 2021, 23:36

Ok, it seem to be working fine but the click command does not register.
2kmentality
Posts: 16
Joined: 21 Apr 2021, 15:21

Re: Running a script on a specific window even while alt tabbed.

Post by 2kmentality » 24 Apr 2021, 00:10

This is my revised code:

Code: Select all

#MaxThreadsperHotkey 2
NumPadAdd::ExitApp

~F1::
Previouskey:= !PreviousKey
Loop
{
If PreviousKey
{
ControlSend,, {F1}, Mob Ragnarok
Sleep, 200
Send, {F1}
Sleep, 150
ControlClick, x405 y325, Mob Ragnarok,, L
Sleep, 200
Send, {F3}
}
Else
Break
}
Return
All commands seems to be working fine aside from the ControlClick command. What am I doing wrong?
User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: Running a script on a specific window even while alt tabbed.

Post by mikeyww » 24 Apr 2021, 05:23

I do not have a way to test it. You can double-check your coordinates by issuing a MouseMove to the same coordinates while the window is active.

You could also change your hotkey in your testing. When you send F1, you may unexpectedly trigger a loop that also exits the routine (intermittently). To prevent the loop, include $ as a hotkey modifier prefix.

Explained: Hotkey modifier symbols
2kmentality
Posts: 16
Joined: 21 Apr 2021, 15:21

Re: Running a script on a specific window even while alt tabbed.

Post by 2kmentality » 25 Apr 2021, 05:03

I double-checked the coordinates using MouseMove but it still doesn't register the command, It clicks where my mouse pointer is located but not on the specified coordinates. Same goes when I change the modifier prefix. I've read somewhere that says ControlClick doesn't always work on all games but I can't confirm it. That being said, is there a possible alternative command for ControlClick?
User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: Running a script on a specific window even while alt tabbed.

Post by mikeyww » 25 Apr 2021, 05:18

Two ideas:

First, since you are indicating that MouseMove works and Click works, you could issue them in sequence, instead of ControlClick (or just move the mouse first, and then issue the click or ControlClick).

Second, the tips for games are sometimes helpful. In some cases, admin mode is needed, or holding the keys or buttons for longer than usual.

https://autohotkey.com/board/topic/111737-how-to-make-ahk-work-in-most-games-the-basics/
Post Reply

Return to “Gaming Help (v1)”