Run script on a specific window only.

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Brillian
Posts: 17
Joined: 06 Nov 2017, 11:45

Run script on a specific window only.

06 Nov 2017, 11:59

Hello everyone! I've been using AHK since last year but I just joined the forums because I can't seem to find any solution to my problem.

Problem: I want to run my script for a specific window and does not effect any other window that I'm currently working on.

I came across this thread: https://autohotkey.com/board/topic/7183 ... ackground/

I do not know what "ahk_id" is. Also, how would I know the program name? Is it the one in the task manager?

Here is my script BTW:
if not A_IsAdmin
{
Run *RunAs "%A_ScriptFullPath%"
ExitApp
}

#Persistent
SetTimer, PressTheKey, 500
SetKeyDelay, 100
Return

PressTheKey:
SendEvent, 1234567890
Return

#If, A_IsPaused
Home::
Suspend
Pause
Return
#If
Home::
Suspend
Pause
Return
Hope someone would help me. Thank you so much!
edesjunior
Posts: 3
Joined: 13 Jul 2017, 08:16

Re: Run script on a specific window only.

06 Nov 2017, 12:34

Hello Brillian,

If I understood correctly, it is not possible. To execute commands like SendEvent the window needs to be in focus.

So that I can execute my scripts without impacting the use of my station, I use a virtual machine.

Sorry if English is not good.

I hope I have helped.

Greetings.
Masha Leeshoee

Re: Run script on a specific window only.

06 Nov 2017, 12:35

Brillian wrote:Hello everyone! I've been using AHK since last year but I just joined the forums because I can't seem to find any solution to my problem.

Problem: I want to run my script for a specific window and does not effect any other window that I'm currently working on.

I came across this thread: https://autohotkey.com/board/topic/7183 ... ackground/

I do not know what "ahk_id" is. Also, how would I know the program name? Is it the one in the task manager?

Here is my script BTW:
if not A_IsAdmin
{
Run *RunAs "%A_ScriptFullPath%"
ExitApp
}

#Persistent
SetTimer, PressTheKey, 500
SetKeyDelay, 100
Return

PressTheKey:
SendEvent, 1234567890
Return

#If, A_IsPaused
Home::
Suspend
Pause
Return
#If
Home::
Suspend
Pause
Return
Hope someone would help me. Thank you so much!
ifwinactive seems to be a good solution for that
Brillian
Posts: 17
Joined: 06 Nov 2017, 11:45

Re: Run script on a specific window only.

06 Nov 2017, 12:50

Thanks for the reply everyone! It seems like I need to replace it with ControlSend. I have no success of doing it. I just want to spam 1234567890 in just one window. Can somebody help me?
help

Re: Run script on a specific window only.

08 Nov 2017, 05:12

not A_IsAdmin
{
Run *RunAs "%A_ScriptFullPath%"
ExitApp
}

#Persistent
SetTimer, PressTheKey, 500
SetKeyDelay, 100
Return

PressTheKey:
IfWinActive, WinTitle ;<- Name of window u want
SendEvent, 1234567890
Return

#If, A_IsPaused
Home::
Suspend
Pause
Return
#If
Home::
Suspend
Pause
Return
Brillian
Posts: 17
Joined: 06 Nov 2017, 11:45

Re: Run script on a specific window only.

08 Nov 2017, 16:45

help wrote:not A_IsAdmin
{
Run *RunAs "%A_ScriptFullPath%"
ExitApp
}

#Persistent
SetTimer, PressTheKey, 500
SetKeyDelay, 100
Return

PressTheKey:
IfWinActive, WinTitle ;<- Name of window u want
SendEvent, 1234567890
Return

#If, A_IsPaused
Home::
Suspend
Pause
Return
#If
Home::
Suspend
Pause
Return
Doesn't work. I want something like this: https://autohotkey.com/board/topic/7183 ... ackground/

Here is the info of the window I want to be affected by the AHK:
Window Title: Rohan
Class: ahk_class Rohan
Process: ahk_exe rohanclient.exe
User avatar
Micromegas
Posts: 260
Joined: 28 Apr 2015, 23:02
Location: Germany

Re: Run script on a specific window only.

23 Nov 2017, 04:33

Have you tried IfWinActive?

Regarding your other questions:
"ahk_id" is described at WinTitle.
To know the right names for WinTitle, use the tool AU3_Spy.exe that comes with AutoHotkey.
Brillian
Posts: 17
Joined: 06 Nov 2017, 11:45

Re: Run script on a specific window only.

23 Nov 2017, 10:00

Thank you for the response, sir. I have a question. Disregarding my codes above. How can I make a new one to have a similar that can work on a selected window? Because I want to do stuffs simultaneously. With the codes I'm currently using, I cannot switch windows (alt tab) to do other works while my game is still pressing the hotkeys.

Btw here is the data that I gathered from using Window Spy:
Window Title: Rohan
Class: ahk_class Rohan
Process: ahk_exe rohanclient.exe
User avatar
Micromegas
Posts: 260
Joined: 28 Apr 2015, 23:02
Location: Germany

Re: Run script on a specific window only.

23 Nov 2017, 10:28

You could do that with IfWinActive, as Masha Leeshoee suggested, but a better and more standard way is with the directive #IfWinActive.
Brillian
Posts: 17
Joined: 06 Nov 2017, 11:45

Re: Run script on a specific window only.

23 Nov 2017, 11:52

Micromegas wrote:You could do that with IfWinActive, as Masha Leeshoee suggested, but a better and more standard way is with the directive #IfWinActive.
Hmmm.. I think that won't work. What I want is to send keys on a specific window so that I can do other things while the script is also doing its job on the window I chose.
Brillian
Posts: 17
Joined: 06 Nov 2017, 11:45

Re: Run script on a specific window only.

23 Nov 2017, 11:58

"if not A_IsAdmin
{
Run *RunAs "%A_ScriptFullPath%"
ExitApp
}

Loop
{
ControlSend, , 1, Untitled - Notepad
Sleep, 300
ControlSend, , 2, Untitled - Notepad
Sleep, 300
ControlSend, , 3, Untitled - Notepad
Sleep, 300
ControlSend, , 4, Untitled - Notepad
Sleep, 300
ControlSend, , 5, Untitled - Notepad
Sleep, 300
ControlSend, , 6, Untitled - Notepad
Sleep, 300
ControlSend, , 7, Untitled - Notepad
Sleep, 300
ControlSend, , 8, Untitled - Notepad
Sleep, 300
ControlSend, , 9, Untitled - Notepad
Sleep, 300
ControlSend, , 0, Untitled - Notepad
Sleep, 300
}

#If, A_IsPaused
Home::
Suspend
Pause
Return
#If
Home::
Suspend
Pause
Return"

Here is the code that I have done. It works perfectly with notepad. But for my game, it only works on login screen, but whenever I'm already logged on. It does not seem to work. What could be the problem?
User avatar
Micromegas
Posts: 260
Joined: 28 Apr 2015, 23:02
Location: Germany

Re: Run script on a specific window only.

23 Nov 2017, 19:44

Brillian wrote:What I want is to send keys on a specific window so that I can do other things while the script is also doing its job on the window I chose.
Let me get that straight. So you want AHK to play the game for you while you're doing something else? Sorry, that sort of thing I've never done; to me that sounds just weird, and I would't want to play with someone who does that. But I'm not an experienced gamer, we have a subforum for Gaming questions; why didn't you ask that there?
Brillian
Posts: 17
Joined: 06 Nov 2017, 11:45

Re: Run script on a specific window only.

24 Nov 2017, 06:09

Not play the game for me. But to press the keys just to spam buffs etc. While I'm doing other things. I made it work with notepad. Tried and tested it is working, it keeps the script running on notepad while I'm doing other things (browsing, typing on chrome, etc). I also ask there but unfortunately, nobody seems to know the solution. It's not just for games anyway, the main prob is to get the script run into a specific window (the focus will not get anywhere else, just there).
User avatar
Micromegas
Posts: 260
Joined: 28 Apr 2015, 23:02
Location: Germany

Re: Run script on a specific window only.

24 Nov 2017, 06:56

I don't know what "spam buffs etc" means and I found no definition for the expression; a quick search gives me such results as https://forum.thesettlersonline.net/thr ... -mail-spam, which sounds like part of playing a game to me.

I've tried to think of a use for acting continuously on a given window for a non-game application, and the first thing that comes to mind is my online banking website, which requires me to acknowledge that I'm still there. Of course, while that can be annoying, they do so for a reason, and I would not advocate having a script do that.

But let's assume there is a legitimate application for what you want to do. It seems to me you're trying to squeeze two entirely different questions into this topic:
  1. Your original question "Run script on a specific window only". How do you expect that to work? In the background (a non-active window), while you do your other thing in the foreground? To me, this seems impossible, since all programs I know assume that they're active when someone interacts with them. Or do you want your other work interrupted again and again as the macro switches between the windows? That you could do with the functions listed in our Content under "Window" (that is, the sidebar of our documentation, such as here).
  2. How to communicate with your specific game. This is the problem you brought up with your Notepad example. That's a question for the Gaming forum.
Brillian
Posts: 17
Joined: 06 Nov 2017, 11:45

Re: Run script on a specific window only.

24 Nov 2017, 07:41

I've read that the controlsend does that. It sends the key strokes on a specific window. Sorry if my post is somewhat confusing. I'll just post a new one in the Gaming forum and recostruct my posts to avoid confusion. I'm sorry and thanks for the reply.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada and 90 guests