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

Viewed 28301

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?

13 Answers

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

Worked for me:

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

It simulates the Alt + Tab behavior for F1 key.

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
}
send {Alt down}{tab}
send {Alt up}

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

  • Sleep 100
  • Send !{Tab}
  • Sleep 100

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

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.
}

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

Related