5
votes

How do you make your alt+tab functionality work like OSX where it uses meta+tab and not alt+tab, while also respecting the shift key?

I have the solution for this. Note the date of this post because I've gone through many apparently obsolete solutions before figuring this out. I've worked on this problem for a total of about 4 hours and have encountered a plethora of solutions that don't work. I'm a professional developer of 10 years but this problem nearly killed me.

3

3 Answers

4
votes

There is a special keyword to replicate Alt + Tab functionality.

I also realized that AutoHotkey will block keyboard signals until you've completed the shortcut by releasing all keys, so any additional tabs that are sent while I held down the control key are ignored.

Yes, it does that. Hotkeys create something they call "threads" and by default there can be only one. So if you want to trigger the same hotkey again before the previous "thread" has finished, you can use #MaxThreadsPerHotkey directive.

The code below swaps Alt + Tab and Ctrl + Tab functionality — that is Ctrl + Tab will switch between apps and Alt + Tab between tabs in a browser. The thread limit is 255 which means that you can hold Ctrl and (in theory) press Tab 255 times before it stops working.

#UseHook
SendMode Input
#MaxThreads 255

#MaxThreadsPerHotkey 255
<!Tab::
Send {LCtrl down}{Tab}
Keywait LAlt
Send {LCtrl up}
return
#MaxThreadsPerHotkey

<^Tab::AltTab

I think I've spent more time trying to make AHK hotkeys do what I want than they could possibly save me even if I lived to a hundred.

-1
votes

Note: I've mapped RControl onto my LWin key via Windows registry, so autohotkey sits on top of that.

I found many solutions that said simply

>^Tab::
   Send {LAlt down}{Tab}
   KeyWait RControl
   Send {LAlt up}
   return

The issue here is that it doesn't handle alt+tab+tab+tab to go 3 programs back.

I also realized that AutoHotKeys will block keyboard signals until you've completed the shortcut by releasing all keys, so any additional tabs that are sent while I held down the control key are ignored.

What I realized is I needed a 2nd AutoHotKeys process to force that through. It seems like a rather redundant thing to write but here's my 2nd script:

#IfWinActive "ahk_class TaskSwitcherWnd"
Tab::Send {Tab}
#IfWinActive

If you're not familiar, the #IfWinActive stuff is just to make it so that this isn't firing every time the Tab key is sent - it's only while Windows' task switcher is in focus. Ultimately, this script simply says Tab::Send {Tab}.

Then you need to tell it to also react to the shift key. Oddly, the task switcher listens for that just fine but not Tab, so we don't need to forward the Shift key in that script too.

Ultimately, my two scripts look like this:

Script 1

#IfWinNotActive ahk_class TaskSwitcherWnd
; Remap Ctrl-Tab to Alt-Tab
$>^Tab::
    Send {Alt down}{Tab}
    Keywait Control
    Send {Alt up}
    return

; Remap Ctrl-Shift-Tab to Alt-Shift-Tab
$>^+Tab::
    Send {Alt down}{Shift down}{Tab}
    Keywait Control
    Send {Alt up}{Shift up}
    return
#IfWinActive

Script 2

#IfWinActive ahk_class TaskSwitcherWnd
Tab::Send {Tab}
#IfWinActive

Run these simultaneously.

-1
votes
<#Tab::AltTab

This will remap "win + tab" to "alt tab". It also works with multiple presses of the tab key.