25

Ok. I know this is a very stupid question. But I'm stuck already for an hour.

I got very little experience with ahk, however I made work every script until now with no problems. I explored the ahk tutorials but found no solution up to now.

I'm trying to switch to prev. app with a single numpad off key. I've tried:

!{Tab}

,

{Alt down}{Tab}{Alt up}

I've tried it with Sleep delays, multiline, multiline inside brackets, with and without commas after commands, etc.

I'm quite sure is very simple but something I've not tried yet.

Any suggestion?

15 Answers15

23
$F1:: AltTab()
$F2:: AltTabMenu()

; AltTab-replacement for Windows 8:
AltTab(){
    list := ""
    WinGet, id, list
    Loop, %id%
    {
        this_ID := id%A_Index%
        IfWinActive, ahk_id %this_ID%
            continue    
        WinGetTitle, title, ahk_id %this_ID%
        If (title = "")
            continue
        If (!IsWindow(WinExist("ahk_id" . this_ID))) 
            continue
        WinActivate, ahk_id %this_ID%
        WinWaitActive, ahk_id %this_ID%,,2 
            break
    }
}

; AltTabMenu-replacement for Windows 8:
AltTabMenu(){
    list := ""
    Menu, windows, Add
    Menu, windows, deleteAll
    WinGet, id, list
    Loop, %id%
    {
        this_ID := id%A_Index%
        WinGetTitle, title, ahk_id %this_ID%
        If (title = "")
            continue            
        If (!IsWindow(WinExist("ahk_id" . this_ID))) 
            continue
        Menu, windows, Add, %title%, ActivateTitle      
        WinGet, Path, ProcessPath, ahk_id %this_ID%
        Try 
            Menu, windows, Icon, %title%, %Path%,, 0
        Catch 
            Menu, windows, Icon, %title%, %A_WinDir%\System32\SHELL32.dll, 3, 0 
    }
    CoordMode, Mouse, Screen
    MouseMove, (0.4*A_ScreenWidth), (0.35*A_ScreenHeight)
    CoordMode, Menu, Screen
    Xm := (0.25*A_ScreenWidth)
    Ym := (0.25*A_ScreenHeight)
    Menu, windows, Show, %Xm%, %Ym%
}

ActivateTitle:
    SetTitleMatchMode 3
    WinActivate, %A_ThisMenuItem%
return

;-----------------------------------------------------------------
; Check whether the target window is activation target
;-----------------------------------------------------------------
IsWindow(hWnd){
    WinGet, dwStyle, Style, ahk_id %hWnd%
    if ((dwStyle&0x08000000) || !(dwStyle&0x10000000)) {
        return false
    }
    WinGet, dwExStyle, ExStyle, ahk_id %hWnd%
    if (dwExStyle & 0x00000080) {
        return false
    }
    WinGetClass, szClass, ahk_id %hWnd%
    if (szClass = "TApplication") {
        return false
    }
    return true
}

EDIT (suggested by the user Ooker):

The script pops up a menu for you to choose.

This is what it looks like:

user3419297
  • 9,537
  • 2
  • 15
  • 24
  • That's exactly what I was looking for! Works great! Thanks! –  Mar 16 '16 at 02:15
  • Is the Windows 10 app switcher that bad? I know you can't use it with the mouse, but I still find it helpful. I don't remember if Windows 8 was any different. – DanGordon Nov 19 '18 at 20:18
  • Windows 10 introduced a different app switcher that you can [use with the mouse](https://stackoverflow.com/questions/48651443/autohotkey-issue-with-alt-tab-shortcut?answertab=active#tab-top). The main advantage of the above code is that you can switch to the previous active window without seeing the switcher. – user3419297 Nov 19 '18 at 21:09
  • So do you mean that when you just want to switch to previous tab, you don't have to see that quick pop-up of all the apps? – DanGordon Nov 20 '18 at 19:35
  • 1
    Yes, that's exactly what I mean. You can try it out yourself. – user3419297 Nov 21 '18 at 17:32
  • Wow, awesome thanks so much! Really useful to switch apps with a VNC remote connection to a server, since all the special key combos get intercepted by the local (client) PC. So this allows to *hack* any other key combo to bypass this restriction. An idea to improve this could be a version to group windows by virtual desktop (right now it gets all windows regardless of which desktop) and perhaps a fuzzy search box (to search & limit the list). Maybe a github project ? – deryb Mar 01 '20 at 18:38
  • @user3419297 AltTabMenu works fine at my Windows 10 pc. Although AltTab does only switch back and forth between 2 programs while I have more programs open. (for example if I have Excel, Chrome and OneNote open it switches only between Excel and Chrome [the last two activated programs it seems]). Do you know what can be the cause? – user2165379 Oct 28 '20 at 13:56
  • @user2165379, what windows version are you on? See also [Alt-Tab Hotkeys](https://www.autohotkey.com/docs/Hotkeys.htm#AltTabDetail). – user3419297 Oct 28 '20 at 20:12
  • @user3419297Thanks. I have Windows 10. If I use your code, AltTabMenu works fine and AltTab() does nothing. If I use other code-examples from answers below such as --- F11:: Send, {ALT DOWN}{TAB}{ALT UP} return --- or --- n::AltTab ---, I can only switch between two open windows while having more than two open. – user2165379 Oct 29 '20 at 13:03
  • This is awesome, but it only works once on my WIndows 10 pro. Chrome seems to be the culprit though. If Chrome is not open the script will cycle just fine. Any ideas how to fix this? – Kalamalka Kid Nov 07 '21 at 05:43
  • @Kalamalka Kid, Try to find out (using Window Spy or a MsgBox) what kind of window (title, class) becomes active in that case and exclude it (return false) in the IsWindow(hWnd) function. – user3419297 Nov 07 '21 at 09:57
10

If you just want to switch back to the previous application, use Send, !{Esc}

Robert Ilbrink
  • 7,738
  • 2
  • 22
  • 32
  • 1
    This solution is better suited when you have a fixed rotation between apps: after done with program A you switch to B, then you switch to C, then switch back to A, and so on. But if you just want an alternative for Alt+Tab, then its lack of GUI selection will make you confused for not knowing where you are. – Ooker Oct 25 '18 at 18:06
  • 1
    Wow! I think I'm a pretty advanced Windows user for so many years, but didn't know of this keyboard shortcut! – Alonzzo2 May 28 '23 at 18:31
7

You shouldn't manually send alt+tab as it is a special windows command, rather use the AltTab commands that do that for you.

AltTabMenu opens the tab menu and selects the program, whileAltTab, ShiftAltTab navigate through it.

h::AltTabMenu  
n::AltTab
m::ShiftAltTab
2501
  • 25,460
  • 4
  • 47
  • 87
  • Well, that seemed like a nice solution, but I couldn't make it work. –  Mar 13 '16 at 17:26
  • I studied it in depth and no matter how I write the script, the key that is supposed to open AltTabMenu or Send Alt+Tab somehow ignores the 'Tab' and the script doesn't work until I press hotkey + real Tab on my keyboard. –  Mar 13 '16 at 17:30
  • @GlebG Open a new empty text file, copy the above code into the file, save as *test.ahk* and run the file. Then press the buttons: `h n m h` – 2501 Mar 13 '16 at 17:34
  • Well, I'm not that stupid haha. I've tried that & all the other examples and it doesn't work unless I press the real Tab key on my keyboard. I've changed it in many ways & still doesn't work. It works in Lua with StrokePlus, but not with Ahk. I understand that 'AltTabMenu' should be something like Alt+Ctrl+Tab. But for some reason Tab is being ignored as a function key in my case (it works as a tab key in text if I use it alone). –  Mar 13 '16 at 18:26
  • @GlebG Sorry, I have no idea of your skill. It is hard to understand *doesn't work*. You can post a link to a complete minimal example that reproduces the problem here in the comments and I'll take a look if you want. – 2501 Mar 13 '16 at 18:31
  • Finally I found some clues. I knew it has something to do with either Windows or driver configuration. I'm on Win8 as well: –  Mar 13 '16 at 19:25
  • https://autohotkey.com/board/topic/84771-alttab-mapping-isnt-working-anymore-in-windows-8/ –  Mar 13 '16 at 19:25
  • https://autohotkey.com/board/topic/86218-how-can-i-emulate-alt-tab-function-on-win-8-using-ahk/ –  Mar 13 '16 at 19:26
  • @GlebG Win8 ignores `AltTab`, `AltTabMenu` ? – 2501 Mar 14 '16 at 08:41
  • 2
    @2501 – Windows 8, AHK 1.1.16.05, elevated – seems that `AltTab`, `ShiftAltTab` are ignored, and `AltTabMenu` leads to strange state (I'll save details here) – miroxlav Mar 15 '16 at 10:17
  • @miroxlav Thank you. Shouldn't Autohotkey implementation fix this? Win8 has been out for a long time. – 2501 Mar 15 '16 at 13:35
4

There are some issues with Windows 8/10 and keys like ctrl-alt-del and alt-tab. Here is one solution:

F1::
  {   
        Send {Alt Down}{Tab} ;Bring up switcher immediately            
        KeyWait, F1, T.5  ; Go to next window; wait .5s before looping
        if (Errorlevel)
       {       
        While ( GetKeyState( "F1","P" ) ) {
            Send {Tab}        
            Sleep, 400 ; wait 400ms before going to next window
        }
    }
        Send {Alt Up} ;Close switcher on hotkey release
}
return
Stepan
  • 1,391
  • 18
  • 40
4

My personal goal was to remap Alt-Tab to Win-Tab (because I'm using a Mac keyboard on a Windows 10) so I took what Stepan wrote above plus some documentation and here is is, working fine for me :

#Tab::
{   
  Send {LAlt Down}{Tab}          
  KeyWait, LWin  ; Wait to release left Win key
  Send {LAlt Up} ; Close switcher on hotkey release
}
return
3

Worked for me:

F1::
Send, {ALT DOWN}{TAB}{ALT UP}
return

It simulates the Alt + Tab behavior for F1 key.

GorvGoyl
  • 42,508
  • 29
  • 229
  • 225
1

Well, finally I found the reason and some "solutions" here and here. It seems that Windows 8 blocks Ahk {Alt Down}{Tab} and AltTabMenu and some other keys.

For now I'm using this to scroll windows forward:

Send !{ESC} 

This to display the AltTabMenu:

Run, "C:\Users\Default\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\Window Switcher.lnk"

And this to switch to the previous app as suggested in one of the topics:

LCtrl & z:: ; AltTabMenu


state := GetKeyState("Capslock", "T")
if state = 1
SetCapsLockState, Off  ; CapsLock On blocks Task Switching metro window

Send, !{Tab}   ; prevents displaying inactive Task Switching metro window
run, Window Switcher.lnk ; must be in script directory otherwise include path 
WinWait, Task Switching,, 2
KeyWait, Ctrl
Send, {Enter}

if state = 1
SetCapsLockState, On  ; restores CapsLock State
state =

return

#IfWinActive, Task Switching
LCtrl & q::Send, {Right}
LCtrl & a::Send, {Left}

It would be great to get to the previous app with no AltTabMenu splashing.

0

In case you want to do multiple "tabs", then the below function should help doing that. This was at least own solution on my Windows 8.1 machine.

The approach is:

  • 1) Get a list of all the windows
  • 2) Loop 1:
    • find the index of the current window
    • set the index to switch to ("current" + "offset")
  • 3) Loop 2:
    • loop until you hit the index to switch to, then switch window

AutoHotKey code sample below:

; Test switch of 1 window
F1::AltTabFunction(offset:=1)

; Test switch of 2 windows
F2::AltTabFunction(offset:=2)

AltTabFunction(offset:=1)
{
    ; ****************************
    ; Function for switching windows by ALT-TAB (offset = number of windows to "tab")
    ; ****************************
    ; Get list of all windows.
    WinGet, AllWinsHwnd, List
    WinGetTitle, active_title, A ; Get title of active window.

    ; Find index of the current window.
    counter_of_none_hidden_windows := 0 ; Initiate counter for counting only the none-hidden windows.
    Loop, % AllWinsHwnd
    {
        ; Find title for window in this loop.
        WinGetTitle, CurrentWinTitle, % "ahk_id " AllWinsHwnd%A_Index%

        ; From [1]: "Retrieves an 8-digit hexadecimal number representing the extended style of a window.".
        ; [1] : https://autohotkey.com/docs/commands/WinGet.htm
        WinGet, exStyle, exStyle, % "ahk_id" AllWinsHwnd%A_Index%

        ; Skip hidden windows by checking exStyle.
        If !(exStyle & 0x100){
            Continue
        }

        ; Window is not hidden. Increase counter.
        counter_of_none_hidden_windows := counter_of_none_hidden_windows+1

        ; Set flag.
        titles_match := CurrentWinTitle = active_title
        If (titles_match) {
            window_index_to_switch_to := counter_of_none_hidden_windows+offset
            break
        }
    }

    ; Find index of the window to switch to and do the actual switch
    counter_of_none_hidden_windows := 0 ; Initiate counter for counting only the none-hidden windows.
    Loop, % AllWinsHwnd
    {
        ; From [1]: "Retrieves an 8-digit hexadecimal number representing the extended style of a window.".
        ; [1] : https://autohotkey.com/docs/commands/WinGet.htm
        WinGet, exStyle, exStyle, % "ahk_id" AllWinsHwnd%A_Index%

        ; Skip hidden windows by checking exStyle.
        If !(exStyle & 0x100){
            Continue
        }

        ; Window is not hidden. Increase counter.
        counter_of_none_hidden_windows := counter_of_none_hidden_windows+1

        ; Set flag.
        found_window_to_switch_to := counter_of_none_hidden_windows = window_index_to_switch_to

        ; Switch window.
        If (found_window_to_switch_to) {
            ; Get title.
            WinGetTitle, CurrentWinTitle, % "ahk_id " AllWinsHwnd%A_Index%
            ; Activate by title.
            WinActivate, %CurrentWinTitle%
            ; Stop loop.
            break
        }
    }
    return ; Nothing to return
}
0
send {Alt down}{tab}
send {Alt up}
dazedandconfused
  • 3,131
  • 1
  • 18
  • 29
0

!{Tab} works to switch between windows if you add sleep before and after it.

  • Sleep 100
  • Send !{Tab}
  • Sleep 100
hywin
  • 11
0

Please refer to this link: https://www.autohotkey.com/docs/Hotkeys.htm#alttab

To cancel the Alt-Tab menu without activating the selected window, press or send Esc. In the following example, you would hold the left Ctrl and press CapsLock to display the menu and advance forward in it. Then you would release the left Ctrl to activate the selected window, or press the mouse wheel to cancel. Define the AltTabWindow window group as shown below before running this example.

LCtrl & CapsLock::AltTab
#IfWinExist ahk_group AltTabWindow  ; Indicates that the alt-tab menu is present on the screen.
MButton::Send {Blind}{Escape}  ; The * prefix allows it to fire whether or not Alt is held down.
#If
0

I've modified the example from the help page on this found here: https://www.autohotkey.com/docs/Hotkeys.htm#AltTabDetail

This was mainly to remap the Windows+Tab key to the Alt+Tab key in this example.

It opens the task view and waits for the user to click, escape or enter. The example from the help page has the alt key getting stuck for me so I changed it to work a little better.

Please let me know if this works for you all.

; Override the Left Win key and tab to Alt + Tab

; Help found here:
; https://www.autohotkey.com/docs/Hotkeys.htm#AltTabDetail
; #IfWinExist ahk_group AltTabWindow

#NoEnv
#SingleInstance force
SendMode Input

LWin & Tab::Send {Alt down}{tab} ; Asterisk is required in this case.
!LButton::
{
  Click
  Send {Alt up}  ; Release the Alt key, which activates the selected window.
}

!Enter::
{
  Send {Alt up}  ; Release the Alt key, which activates the selected window.
}

~*Esc::
{
  Send {Alt up}  ; When the menu is cancelled, release the Alt key automatically.
  ;*Esc::Send {Esc}{Alt up}  ; Without tilde (~), Escape would need to be sent.
}
Ste
  • 1,729
  • 1
  • 17
  • 27
0

I got ALT TAB to work with F1

  • By pressing F1 you can switch to the last window
  • While F1 is kept pressed you can move around with arrow keys or tab to select the window you need.

Code:

`F1::
Send, {ALT DOWN}{TAB}{TAB UP}   ; If F1 is down it invokes the menu for switching windows.
KeyWait, F1                     ; Show menu for switching windows by keeping ALT down until user physically releases F1.
Send, {ALT UP}                  ; If F1 is released release ALT key
return`

Documentation links

KeyWait
KeyList

0x-de1
  • 1
  • 1
0

The simple solution for newbies like me arriving here from a web search:

Send, !{Tab}

Explanation:

I was trying to send an Alt+Tab keystroke without the "Send" command, just like how it was written in the original question, but that doesn't work:
!{Tab}

In Autohotkey you generally need a line that has a command, then a parameter, like these examples:
Run, notepad.exe
Sleep, 1000
Send, Hello

So obviously, if you want to send the Alt+Tab keys, you have to use the same format:
Send, !{Tab}
(Though the comma is optional, it makes it easier for newbies like me to understand what's happening.)

This works on windows 10 and accomplishes exactly what was originally requested and no more ("switch to prev. app"). (I know some other answers give more complicated alt+tab menu replacements but that's not what I was looking for, and technically not what the original question asked.) I also realize some alternatives like Alt+Esc might function better than Alt+Tab, but if you do want to Alt+Tab, that's the simple answer.

To get ahead of potential downvotes: I'm not sure if it matters but I am using an admin account on Windows with UAC turned off. And if you have other things after the alt+tab, I added a Sleep, 400 on the next line to give it time to close the popup and bring the last application to the foreground.

gamingexpert13
  • 346
  • 5
  • 6
-3

I think this question was meant to be a simple request of: how to alt tab in Win10 using AHK, since win10 changed things up? -> I found the most simple solution as shown below... the code makes it necessary to keep the alt key down while the Win10 emu is open - then use the arrow keys an additional number of tabs (if you need three alt tabs, then it's "alt tab, then right 2", see?

macro key name::

{
        Sleep 100
        Send, ^c
        Sleep 1000
        Send, {alt down}{tab}
        Sleep 400
        Send, {right 2}{alt up}
        Sleep 400
        Send, ^v
        Sleep 400
}

So just play with this snip in your code and you can jump passed the 'next' window(s) open. Rossman

Cadoiz
  • 1,446
  • 21
  • 31