Problem with a WinActive and WinNotExist loop script

Ask gaming related questions (AHK v1.1 and older)
uchihito
Posts: 5
Joined: 23 Apr 2024, 17:15
Contact:

Problem with a WinActive and WinNotExist loop script

23 Apr 2024, 17:28

Hi,

I just did a loop for a game and wanted to send the keys when the windows is not active. I tested it with notepad and it works but the problems is when I change the PID to the game´s PID, it doesnt work. Any reason why? any solution? I have another script with "Send" command and it works too but only when the windows si active and I dont want that.

here´s the code:

Code: Select all

#IfWinNotexist, ahk_pid 1592

SendMode Input 

<+F1::SetTimer, AFKMode, % (i:=!i) ? "" : "Off"    
AFKMode: 
    ControlSend, Edit1, {a down}, ahk_pid 1592
    Sleep 2000
    ControlSend, Edit1, {a up}, ahk_pid 1592
    ControlSend, Edit1, {d down}, ahk_pid 1592
    Sleep 2000
    ControlSend, Edit1, {d up}, ahk_pid 1592
return
and there is the one that works but only when windows is active:

Code: Select all

#IfWinActive, ahk_exe proven_ground_client.exe

SendMode Input 

<+F1::SetTimer, AFKMode, % (i:=!i) ? "" : "Off"    
AFKMode: 
    Send {a down}
    Sleep 2000
    Send {a up}
    Send {d down}
    Sleep 2000
    Send {d up}
return

thanks in advance


[Mod action: Moved topic to the “Gaming” section.]
User avatar
boiler
Posts: 17120
Joined: 21 Dec 2014, 02:44

Re: Problem with a WinActive and WinNotExist loop script

23 Apr 2024, 17:42

First, you should never hard-code in a PID because they change each time you run an application. Also, is there really a control named Edit1 like in Notepad? I would doubt it.

Not all windows respond to virtual key presses, and even when they do, some don’t respond to virtual keypresses sent to their window when it’s not active.
uchihito
Posts: 5
Joined: 23 Apr 2024, 17:15
Contact:

Re: Problem with a WinActive and WinNotExist loop script

24 Apr 2024, 21:47

How can I do to a specific process If I have 2 process called the same?
User avatar
boiler
Posts: 17120
Joined: 21 Dec 2014, 02:44

Re: Problem with a WinActive and WinNotExist loop script

24 Apr 2024, 22:03

Are you really meaning that you want to differentiate between the windows? Get the HWND (unique window IDs) of both windows using WinGet…List. If you need to know which one is which, you can compare the window positions or track as each one is opened, or potentially many other aspects that would distinguish them.
uchihito
Posts: 5
Joined: 23 Apr 2024, 17:15
Contact:

Re: Problem with a WinActive and WinNotExist loop script

25 Apr 2024, 05:33

I changed to this, works with notepad but with the game it doesn´t.

Code: Select all

#IfWinNotActive, ahk_pid 8084

SendMode Input 

<+F1::SetTimer, AFKMode, % (i:=!i) ? "" : "Off"    
AFKMode: 
    	ControlSend, , {a down}, ahk_pid 8084
    	Sleep 2000
	ControlSend, , {a up}, ahk_pid 8084
	ControlSend, , {d down}, ahk_pid 8084
    	Sleep 2000
	ControlSend, , {d up}, ahk_pid 8084
return
is there another way to send keys to the windows? I know it works but only when the windows is active but I need it NotActive, minimized, in another monitor
User avatar
boiler
Posts: 17120
Joined: 21 Dec 2014, 02:44

Re: Problem with a WinActive and WinNotExist loop script

25 Apr 2024, 06:13

Again, hard-coding the PID is not the way to go because it changes every time you run the app. And you didn’t say before that you want to send it to a minimized window. I don’t expect you’d ever get that to work with any approach.
uchihito
Posts: 5
Joined: 23 Apr 2024, 17:15
Contact:

Re: Problem with a WinActive and WinNotExist loop script

25 Apr 2024, 06:18

the thing about PID im gonna change it when I see that it works first. And with the minimized windows maybe not, just Not Active windows, in the second monitor in Borderless Windowed, should it work too? or with another cmd?
User avatar
boiler
Posts: 17120
Joined: 21 Dec 2014, 02:44

Re: Problem with a WinActive and WinNotExist loop script

25 Apr 2024, 06:46

uchihito wrote:
25 Apr 2024, 06:18
And with the minimized windows maybe not, just Not Active windows, in the second monitor in Borderless Windowed, should it work too? or with another cmd?
Should it work? As I had posted earlier:
boiler wrote:
23 Apr 2024, 17:42
Not all windows respond to virtual key presses, and even when they do, some don’t respond to virtual keypresses sent to their window when it’s not active.
uchihito
Posts: 5
Joined: 23 Apr 2024, 17:15
Contact:

Re: Problem with a WinActive and WinNotExist loop script

25 Apr 2024, 07:00

So, there is not another way with AutoHotKey right? there not exist another command to send keys to Not active windows? Just ControlSend?
User avatar
boiler
Posts: 17120
Joined: 21 Dec 2014, 02:44

Re: Problem with a WinActive and WinNotExist loop script

25 Apr 2024, 07:56

That's really it. It's not an AHK limitation. No other tool would be able to send keystrokes in a way that will get it to accept them, if that's really the issue here. It's the way the app works. I could easily write a little app that will only respond to keystrokes when its window is active and there would be nothing anyone could do about it unless they changed the code of the app itself. That could be the only approach here, which it quite advanced -- to inject code into the application to have it behave differently. I don't think you'll get anyone to help you with that.

The only other thing I would suggest trying is to run your script as administrator or otherwise address UAC.

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 29 guests