7

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 Answers 3

7

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
  • This seems like a really nice solution. I'm going to go ahead and accept this but I'm still using the same two scripts I made a year ago and they've worked for me, so I'll leave mine be.
    – Tim
    Mar 17, 2021 at 0:01
1
<#Tab::AltTab

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

2
  • Now, how do you remap Shift+Alt+Tab to Shift+Win+Tab ? I've tried it and can't get it to work because when you add the shift modifer on the left, it throws an error when you try to run the script. Apr 1, 2022 at 3:45
  • For example, this does not work +<#Tab::ShiftAltTab Apr 1, 2022 at 4:21
-1

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.

3
  • Your implementation looks very questionable, did you take a look at this on the documentation? Also, #IfWin[Not]Active doesn't take an expression, so you don't quote the string you pass into it. If that actually still works for you even with that mistake, I really don't know why/how.
    – 0x464e
    Jun 8, 2020 at 6:38
  • Thanks for the notes. I'm pretty new to all of this and am only trying to save someone a headache, so I really hope I didn't mess this up too bad. That quoted string came from an example on how to use IfWinActive. I thought I had unquoted it. Either way, I've updated the answer. I tested it and it appears to work. I did see that documentation. The issue was that each possibility seems to be bugged in its own, unique way. For example, >^TabM::AltTabMenu doesn't go away when I release control because ctrl+alt+tab has that behavior. Trust me, I tried quite hard to do it the recommended way.
    – Tim
    Jun 8, 2020 at 11:04
  • Yeah, good stuff that you managed to get a working solution. Though I'm sure there's a prettier way to do it as well. I'll maybe give it a try later if I have nothing better to do.
    – 0x464e
    Jun 8, 2020 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.