Windows virtual desktops. Cycle back to first desktop at end

autohotkeydesktopproductivityvirtual-desktopwindows 10

Let's say I have three virtual desktops. I know I can navigate through them using:

ctrl + windows + left | right

However, is there a way to make it cycle back through to the beginning if I reach the end? So that if I am on desktop 3 "ctrl + windows + right" will take me to desktop 1?

I know this may be possible with an AutoHotkey script. But I don't know how to make that work.

The reason I need this to work this way is because I intend to map this functionality to a mouse I have just bought (it has extra buttons).Currently I need to use two buttons so that I can move backwards and forwards between desktops. I would rather just one button was needed. Hopefully this information helps.

Thanks

Best Answer

This is just an idea for something to try and adapt... not tested.

As long as the script is running it just saves the virtual desktop active in a variable, don't need an ini file necessarily.

The script either has to be started with virtual desktop #1 active or it has to force a sync. The forcing function assumes you can press ctrl+win+left as many times as you want and it will not loop past the first virtual desktop once it gets there.

The shortcut keys trigger on the default windows keys but don't capture them (i.e., tilde ~ lets the keystroke go through). So it would track the keyboard if you were using the keyboard, and if you were using the mouse you could assign the mouse to one shortcut key or another.

You could also add a custom duplicate shortcut for one or both directions... showing this as ctrl+alt+F8, for example in case your mouse button needs to be assigned to something other than the default windows keys for switching between desktops.

#NoEnv
#Persistent

numDesktops := 3  ; set to match number of virtual desktops
if forceSyncAtStartup := True   ; set to False to disable sync on startup
    SendInput % "^#{Left" (numDesktops-1) "}"

vDesktop := 1   ; this must match the virtual desktop active when program starts if a sync isn't forced
return

^!F8::             ; random/custom shortcut for the mouse if desired
~^#Left::
    vDesktop -= 1
    if (vDesktop=0) {
        vDesktop := numDesktops
        sleep 20        ; optional for better reliability

        ; use this if no delay needed for reliable operation
        SendInput % "^#{Right " (numDesktops-1) "}

        ; use this type of setup if a delay is needed
        ; Loop, % (numDesktops-1) {
        ;   SendInput ^#{Right}
        ;   sleep 100       ; adjust for reliability
        ; }
    }
return

~^#Right::
    vDesktop += 1
    sleep 20
    if (vDesktop=(numDesktops+1)) {
        vDesktop := 1
        SendInput % "^#{Left" (numDesktops-1) "}"
    }
return  
Related Question