AutoHotKey - Custom shortcut to switch to next/previous desktop in Windows 10
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 videos on Youtube
MattsterJames
Updated on September 18, 2022Comments
-
MattsterJames almost 2 years
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.
-
Jacob Stanley over 7 yearsI was trying to do something similar and I had the same issue (control key getting stuck, needing to click somewhere on the desktop), the fix for me was to make sure I was using the left/right variants of the windows key and the control key when detecting a key press. This possibly isn't a solution to the above problem per-se, but I thought I'd leave a note here in case anyone else was having the same issue. Here is the solution which worked for my case, using
LWin+LCtrl+j/k
to switch between desktops:<^<#j::Send, ^#{Right}
and<^<#k::Send, ^#{Left}
-
-
MattsterJames almost 9 yearsYes it does, but what I'm trying to do is add my own custom way of switching desktops because I find it somewhat inconvenient to have to use both hands to switch the desktop. I want to be able to use the windows key + Left Alt/Left CTRL to go back and forth between desktops.
-
Michael B almost 9 years@MattsterJames Probably worth amending your question to contain that - I doubt I'm the only person that will read it and think that!
-
MattsterJames almost 9 yearsI guess I forgot to mention that.. Thanks for pointing that out! I edited it.
-
jiggunjer over 7 yearsdid you even read the question before you double-posted?
-
AliOsat Mostafavi over 7 yearsyes i read in question: Or maybe, is there a different way that I can switch desktops without simulating the default Win 10 shortcuts? and also in question: ** This code works, but only if you click a window or anything else on the current desktop before pressing the keys** means my code works without need to click any where !!!!!
-
fohrums almost 6 yearsThis works for me (being able to switch desktops while still holding on winkey), but I want to be able to change the hotkey instead of using the left side:
Win+Alt / Win+LCtrl
. I would prefer to use!^+H / !^+L
. I personally tried to substitute it myself but keywait doesn't seem to apply here while I have to let go of all modifiers and do the other hotkey for it to work (I can't hold it). -
fohrums almost 6 yearsBy removing the
KeyWait
I can now use!^+H / !^+L
while still holding it, Thanks Kevin.