WinActivate terminating or closing game window. Topic is solved

Ask gaming related questions (AHK v1.1 and older)
tedo
Posts: 6
Joined: 12 Dec 2014, 13:19

WinActivate terminating or closing game window.

23 Jul 2022, 16:23

I have a game where I need to open multiple instances of it in order to simultaneously play multiple characters. For some reason WinActivate terminates the deactivated window when it moves from one to activating another. Is there a solution to this where I can use WinActivate? Perhaps someone can advise on investigating what's going on.

I tried a work around with alt-tab as in the code section. But it's slow, I had to slow it down so much for it to work. It also doesn't work well with Edge browser tabs. It's a little buggy overall. Maybe more verification checks would improve it. But I'd rather use WinActivate as it's fast and simple to use.

Code: Select all

altTab(matchTitle) {
    a := GetAltTabWindows()
    for index, value in a {
        WinGetTitle, OutputVar, AHK_id %value%
        FoundPos := InStr(OutputVar, matchTitle)
        If (FoundPos == 1) {
            a := 0
            s := "{Alt Down}"
            index -= 1
            Loop, %index% {
                s .= "{Tab}"
            }
            s .= "{Alt Up}"
			delay := A_KeyDelay
			duration := A_KeyDuration 
			SetKeyDelay, 500, 1
            SendEvent, %s%
			SetKeyDelay, delay, duration
            Return
        }
    }
}

GetAltTabWindows() { ; teadrinker https://www.autohotkey.com/boards/viewtopic.php?p=344440#p344440
   AltTabList := []
   WinGet, list, List
   Loop % list
      if IsAltTabWindow(list%A_Index%)
         AltTabList.Push(list%A_Index%)
   Return AltTabList
}

IsAltTabWindow(hWnd) {
   static GA_ROOTOWNER := 3, WS_EX_APPWINDOW := 0x40000, WS_EX_TOOLWINDOW := 0x80, DWMWA_CLOAKED := 14
   if !DllCall("IsWindowVisible", "Ptr", hWnd)
      Return false
   
   hOwner := DllCall("GetAncestor", "Ptr", hWnd, "UInt", GA_ROOTOWNER, "Ptr")
   hPopup := DllCall("GetLastActivePopup", "Ptr", hOwner, "Ptr")
   if (hOwner = hWnd && hPopup != hWnd)
      Return false
   
   WinGet, exStyles, ExStyle, ahk_id %hWnd%
   ;if (exStyles & WS_EX_TOOLWINDOW) && !(exStyles & WS_EX_APPWINDOW)
   if (exStyles & 0x8000088) ; WS_EX_TOPMOST | WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE
      Return false
      
   DllCall("DwmApi\DwmGetWindowAttribute", "Ptr", hWnd, "UInt", DWMWA_CLOAKED, "UIntP", cloaked, "UInt", 4)
   Return !cloaked
}
thinkRand
Posts: 44
Joined: 02 Mar 2022, 04:16
Location: Somewhere in Venezuela
Contact:

Re: WinActivate terminating or closing game window.

23 Jul 2022, 21:18

Hope this can help, bro
I asumed you wanted to switch between chracters. Is it?

Code: Select all

CoordMode, Mouse, Screen
character := 0
index := 1
return

+LButton:: ;do shift + click when hovering over the game to this to work
	MouseGetPos, _, _, win
	WinGet, gameProcess, ProcessName , ahk_id %win%
	WinGet, character, list , ahk_exe %gameProcess%
return

space::
	if !character
		return
	
	temp := character%index%
	WinActivate, ahk_id %temp%
	index++
	if (index > character){
		index := 1 
	}
return


Discord: thinkRand#7433 (UID 681695022600814642)
tedo
Posts: 6
Joined: 12 Dec 2014, 13:19

Re: WinActivate terminating or closing game window.

24 Jul 2022, 03:23

Thank you thinkRand for writing up that code. I learned new things!

Sadly it's still the same problem. I got VSCode and a .ahk debug extension(Mark Wiemer's) so I can see when the problem happens and it's on WinActivate.

This works as expected >>> Using WinActivate to swap between the game window and other non-related windows.

This termites/closes the window that just lost focus while switching to the next window >>> Using WinActivate to swap between two or more windows for the same game. Again, I'm playing multiple characters and you can only have on character per a game window.
thinkRand
Posts: 44
Joined: 02 Mar 2022, 04:16
Location: Somewhere in Venezuela
Contact:

Re: WinActivate terminating or closing game window.

26 Jul 2022, 11:40

Best of luck!
thinkRand wrote:
23 Jul 2022, 21:18
Hope this can help, bro
I asumed you wanted to switch between chracters. Is it?

Code: Select all

CoordMode, Mouse, Screen
character := 0
index := 1
return

+LButton:: ;do shift + click when hovering over the game to this to work
	MouseGetPos, _, _, win
	WinGet, gameProcess, ProcessName , ahk_id %win%
	WinGet, character, list , ahk_exe %gameProcess%
return

space::
	if !character
		return
	
	temp := character%index%
	DllCall("ShowWindow", "Ptr", temp,"Int", 8)
	index++
	if (index > character){
		index := 1 
	}
return


Discord: thinkRand#7433 (UID 681695022600814642)
tedo
Posts: 6
Joined: 12 Dec 2014, 13:19

Re: WinActivate terminating or closing game window.  Topic is solved

28 Jul 2022, 01:28

I found a work around. I still don't' know why WinActivate is closing/terminating a window.

The work around is to make a GUI window and swap to that before going to the next game window. Here is a modified version of thinkRand's script.

Code: Select all

#SingleInstance, Force
SendMode Input
SetWorkingDir, %A_ScriptDir%
CoordMode, Mouse, Screen
character := 0
index := 1
Gui, New, HwndswaperHWND
Gui,show
return

+LButton:: ;do shift + click when hovering over the game to this to work
	MouseGetPos, _, _, win
	WinGet, gameProcess, ProcessName , ahk_id %win%
	WinGet, character, list , ahk_exe %gameProcess%
return

space::
	if !character
		return
	
	temp := character%index%
	WinActivate, ahk_id %swaperHWND%
	WinActivate, ahk_id %temp%
	index++
	if (index > character){
		index := 1 
	}
return
thinkRand
Posts: 44
Joined: 02 Mar 2022, 04:16
Location: Somewhere in Venezuela
Contact:

Re: WinActivate terminating or closing game window.

03 Aug 2022, 10:21

Nice! This topic is solved
Discord: thinkRand#7433 (UID 681695022600814642)

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], mexican scientist and 75 guests