[SOLVED] Can I toggle between two windows with one shortcut?

Get help with using AutoHotkey and its commands and hotkeys
andersbrontosaurus
Posts: 3
Joined: 24 Oct 2015, 17:44

[SOLVED] Can I toggle between two windows with one shortcut?

25 Oct 2015, 18:00

Hello!
I'm new to autohotkey.
My intention is to use it with the tracker Jeskola Buzz.
My first issue is toggling between two windows. I want them on the screen side by side. To be able to control them the window have to be active and I would like to activate them without fiddling with the mouse.
With the help of tutorials and manuals I have learnt how to create hotkeys to activate windows so I now have two hotkeys, one for each window. My question is: is there a way to toggle between the two windows using only one hotkey? I mean, in a similar way that alt+tab can be used to toggle between active programs on the computer, could this be done inside a program?
Last edited by andersbrontosaurus on 26 Oct 2015, 17:27, edited 2 times in total.
User avatar
Nextron
Posts: 1378
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Can I toggle betwwen two windows with one shortcut?

25 Oct 2015, 18:37

This should get you started:

Code: Select all

F1::
	QuickToggle:=!QuickToggle
	If (QuickToggle)
		WinActivate App1
	Else
		WinActivate App2
Return
lexikos
Posts: 7088
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Can I toggle betwwen two windows with one shortcut?

26 Oct 2015, 03:04

Nextron's script falls short if you manually activate one of the windows - i.e. you might need to press the hotkey twice to have an effect.

Another way is to check which window is active:

Code: Select all

F1::
    if !WinActive("App1")
        WinActivate App1
    else if !WinActive("App2")
        WinActivate App2
return
App1 and App2 are window titles, as in Nextron's script. You can extend this to work with as many windows as you want by simply adding one else if..WinActivate for each window.

But actually, there's a built-in command for this: GroupActivate.

Code: Select all

GroupAdd Swap, App1
GroupAdd Swap, App2
; Add as many window titles to the group as you want.

F1::GroupActivate Swap
Once again, App1 and App2 are window titles. Swap is just a name I chose. Because the hotkey only calls one command, we can write it on one line and omit return. GroupAdd should be placed in the auto-execute section. (It can be called from anywhere, but if you put it below a hotkey it probably won't be called at all.)
andersbrontosaurus
Posts: 3
Joined: 24 Oct 2015, 17:44

Re: Can I toggle between two windows with one shortcut?

26 Oct 2015, 05:30

@Nextron @lexikos thanks a lot for your input! This will keep my brain occupied today.
Lexikos, the way I as a novice understand your first script it activates the already active window. Maybe it has something to do with the "!" in the beginning of !ifwinactive. I need to learn what those prefixes do...

...but groupactivate seems perfect for what I need.
User avatar
Nextron
Posts: 1378
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: Can I toggle between two windows with one shortcut?

26 Oct 2015, 07:12

The ! basically means 'not'. It interprets the next value as binary and returns the opposite value. So in this case, if app1 is not active, activate it. Although I'm not seeing the extensibility to a third window in either my or Lexikos' example; another if else would only execute if all former windows are simultaneously active.
I did not know about the GroupActivate, I'd stick with that one too.
lexikos
Posts: 7088
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Can I toggle between two windows with one shortcut?

26 Oct 2015, 16:29

... another if else would only execute if all former windows are simultaneously active.
You're right, that was silly of me.
andersbrontosaurus
Posts: 3
Joined: 24 Oct 2015, 17:44

Re: Can I toggle between two windows with one shortcut?

26 Oct 2015, 17:27

Sweet as!
Groupaddon works perfect for my need.
Thanks a lot!

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], digdugdirk, doubledave22, TAC109, timebones and 60 guests