hotkey toggle window between foreground and background? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
redrum
Posts: 74
Joined: 25 Jan 2015, 22:57

hotkey toggle window between foreground and background?

14 Jun 2021, 10:11

I have a short script that activates a window. I only really need to bring the window to the foreground, but activating the window is the only way I know how to accomplish the goal.

Code: Select all

!+a::  ; SHIFT+ALT+a
If WinExist("Message Center Main@thinkorswim")
	{WinActivate ;
	}
Return	
What I'd really like to do is use the same hotkey to toggle between moving this window to the foreground and to the background. Failing that, one hotkey to bring to foreground and another hotkey to send to background is also fine.

Is there a way to accomplish this in AHK?
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: hotkey toggle window between foreground and background?  Topic is solved

14 Jun 2021, 10:32

Code: Select all

#IfWinExist Message Center Main@thinkorswim
!+a:: ; ALT+SHIFT+a
If WinActive(wTitle := "Message Center Main@thinkorswim")
 WinMinimize
Else WinActivate, %wTitle%
Return
#IfWinExist
Last edited by mikeyww on 14 Jun 2021, 10:40, edited 2 times in total.
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: hotkey toggle window between foreground and background?

14 Jun 2021, 10:35

Code: Select all

ontoptoggle:=false

!+a:: winset,  % (OnTopToggle:=!OnTopToggle)? "Top" : "Bottom", , Message Center Main@thinkorswim
Last edited by braunbaer on 14 Jun 2021, 10:47, edited 1 time in total.
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: hotkey toggle window between foreground and background?

14 Jun 2021, 10:37

That one did not work when I tested in Notepad. At least, the Notepad window did not become visible.
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: hotkey toggle window between foreground and background?

14 Jun 2021, 10:48

Sorry, forgot a comma. I added the comma and then tested it (not with notepad, but with AHK help) , it works.
Strange enough, it really does not bring notepad to the foreground. With notepad, Bottom works, Top doesn't.
Last edited by braunbaer on 14 Jun 2021, 10:55, edited 2 times in total.
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: hotkey toggle window between foreground and background?

14 Jun 2021, 10:51

It did not work here, but who knows, perhaps my system is not typical!

Also noted:
In addition, this sub-command may have no effect due to the operating system's protection against applications that try to steal focus from the user (it may depend on factors such as what type of window is currently active and what the user is currently doing).
redrum
Posts: 74
Joined: 25 Jan 2015, 22:57

Re: hotkey toggle window between foreground and background?

14 Jun 2021, 11:29

mikeyww wrote:
14 Jun 2021, 10:32

Code: Select all

#IfWinExist Message Center Main@thinkorswim
!+a:: ; ALT+SHIFT+a
If WinActive(wTitle := "Message Center Main@thinkorswim")
 WinMinimize
Else WinActivate, %wTitle%
Return
#IfWinExist
Thanks, that works.

Interestingly, with the #IfWinExist check, the hotkey stops working after the window is minimized. If I make another window active, then the hotkey works again.

But if I delete #IfWinExist and use:

Code: Select all

!+a:: ; ALT+SHIFT+a
If WinActive(wTitle := "Message Center Main@thinkorswim")
 WinMinimize
Else WinActivate, %wTitle%
Return
. . . the hotkey works perfectly with unlimited toggling.
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: hotkey toggle window between foreground and background?

14 Jun 2021, 11:34

OK! I'm glad that you have it working.
Last edited by mikeyww on 14 Jun 2021, 11:36, edited 1 time in total.
redrum
Posts: 74
Joined: 25 Jan 2015, 22:57

Re: hotkey toggle window between foreground and background?

14 Jun 2021, 11:35

mikeyww wrote:
14 Jun 2021, 11:34
OK. My hunch is that your program changes the window title when the window is not active. That would explain your finding.
Makes sense, thanks.
User avatar
mikeyww
Posts: 26848
Joined: 09 Sep 2014, 18:38

Re: hotkey toggle window between foreground and background?

14 Jun 2021, 11:37

I changed my post because... I'm not sure how you could activate the window if it did not also exist (unless hidden somehow).... Oh, well!
redrum
Posts: 74
Joined: 25 Jan 2015, 22:57

Re: hotkey toggle window between foreground and background?

14 Jun 2021, 14:41

mikeyww wrote:
14 Jun 2021, 11:37
I changed my post because... I'm not sure how you could activate the window if it did not also exist (unless hidden somehow).... Oh, well!
Yeah, I don't know, but it's working fine, so better to let this sleeping dog lie! :beer:
benjaloo
Posts: 3
Joined: 28 May 2022, 08:15

Re: hotkey toggle window between foreground and background?

28 May 2022, 08:20

Just wanted to say thank you for this. This is exactly what I needed and this code worked perfectly to toggle a single-window app in and out.

Code: Select all

!+a:: ; ALT+SHIFT+a
If WinActive(wTitle := "Message Center Main@thinkorswim")
 WinMinimize
Else WinActivate, %wTitle%
Return

Thanks!!!!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ahabse7en and 254 guests