Windows – AutoHotKey – Custom shortcut to switch to next/previous desktop in Windows 10

autohotkeykeyboard shortcutsvirtual-desktopwindows 10

I have written a small AutoHotKey script to switch virtual desktops in Windows 10. I wrote this code so that I could switch desktops with my own custom keyboard shortcut instead of using Windows 10's default shortcut (WIN + CTRL + LEFT/RIGHT).

Code:

#LAlt::^#Right ; switch to next desktop with Windows key + Left Alt key
#LCtrl::^#Left ; switch to next desktop with Windows key + Left CTRL key

This code works, but only if you click a window or anything else on the current desktop before pressing the keys. I tried using the Click function to simulate a click before it switched desktops (which worked), but it moved the mouse to the coordinates that I clicked, which obviously is not what I want. I've played around with ControlClick a bit, but never got that to work.

So my question is this: is there a way that I can set the focus on the current desktop before executing the command to switch desktops? Or maybe, is there a different way that I can switch desktops without simulating the default Win 10 shortcuts?

Thanks!


EDIT 1

One thing I have noticed is that if I click the taskbar, and THEN do LWin+LAlt/LCtrl, I can switch back and forth smoothly by holding down LWin and alternating between LAlt and LCtrl.

When I click the taskbar and then do LWin+LAlt, this is what KeyHistory spits out:

VK  SC  Type    Up/Dn   Elapsed Key     Window
----------------------------------------------
5B  15B     d   1.91    LWin            
A4  038 h   d   0.31    LAlt            
A2  01D i   d   0.00    LControl        
A2  01D i   u   0.00    LControl        
A4  038 i   u   0.00    LAlt            
A2  01D i   d   0.05    LControl        
5B  15B i   d   0.02    LWin            
27  14D i   d   0.02    Right           
27  14D i   u   0.00    Right           
A2  01D i   u   0.01    LControl        
5B  15B i   u   0.02    LWin            
A2  01D i   d   0.01    LControl        
5B  15B i   d   0.00    LWin            
A2  01D i   u   0.00    LControl        
A4  038 s   u   0.00    LAlt            
5B  15B     u   0.06    LWin            
A2  01D i   d   0.00    LControl        
A2  01D i   u   0.00    LControl
(This is what I want it to do without clicking the taskbar)

But when I JUST hit LWin+LAlt, KeyHistory shows that the key events stop after the "Right Up" event:

VK  SC  Type    Up/Dn   Elapsed Key     Window
----------------------------------------------
5B  15B     d   1.91    LWin            
A4  038 h   d   0.31    LAlt            
A2  01D i   d   0.00    LControl        
A2  01D i   u   0.00    LControl        
A4  038 i   u   0.00    LAlt            
A2  01D i   d   0.05    LControl        
5B  15B i   d   0.02    LWin            
27  14D i   d   0.02    Right           
27  14D i   u   0.00    Right
(There should be more after this)

The LControl Up event is never fired, and that seems to screw everything up.

Best Answer

Sometimes when you are trying to send modifier keys (win/ctrl/alt) and the triggering string has modifier keys as well, you need to wait for the triggering keys to be released, or they will affect the replacement string as you are finding out.

Try using KeyWait to accomplish this. Notice we are now using hotkey syntax vs hotstring

#LAlt:: ; switch to next desktop with Windows key + Left Alt key
  KeyWait LAlt
  SendInput #^{Right}
  Return

#LCtrl:: ; switch to previous desktop with Windows key + Left CTRL key
  KeyWait LCtrl
  SendInput #^{Left}
  Return

For purposes of switching desktops the above worked for me.

On other occasions, there are times when even this approach doesn't work, and there is another possible solution. Instead of key waits like these...

KeyWait LAlt
KeyWait LCtrl

...replace with the corresponding one of these keystrokes to clear the state of the key:

Send,{LAlt Down}{LAlt Up}
Send,{LCtrl Down}{LCtrl Up}
Related Question