Search code examples
autohotkey

What is the right way to send Alt + Tab in Ahk?


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?


Solution

  • $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: