9

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.

1
  • I 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} Nov 6, 2016 at 7:00

4 Answers 4

3

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}
2
  • This 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
    Sep 13, 2018 at 8:09
  • 1
    By removing the KeyWait I can now use !^+H / !^+L while still holding it, Thanks Kevin.
    – fohrums
    Sep 13, 2018 at 8:21
0

Unless I'm missing a point somewhere, doesn't Windows + ctrl + / arrow switch desktops? At least it does on mine.

3
  • 2
    Yes 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. Aug 5, 2015 at 20:17
  • 3
    @MattsterJames Probably worth amending your question to contain that - I doubt I'm the only person that will read it and think that!
    – Michael B
    Aug 5, 2015 at 20:19
  • I guess I forgot to mention that.. Thanks for pointing that out! I edited it. Aug 5, 2015 at 20:24
0

Don't know if it's still relevant, but 'pzone' from Reddit wrote a script which use a function with a global variable that remembers which desktop you are on, and that way you can bind one hotkey to toggle between two desktops. It will only work if you have 2 desktops.

switchedDesktop := false
switchDesktop() 
{
  global switchedDesktop
    if switchedDesktop
    {
        SendEvent ^#{Right}
        switchedDesktop := false
    }
    else
    {
        SendEvent ^#{Left}
        switchedDesktop := true
    }
}

https://www.reddit.com/r/AutoHotkey/comments/3fyudo/automatically_switch_between_windows_10_virtual

-1

For example i want to set Page Down for next desktop and Page Up for previous desktop After i install AutoHotkey , i create a new AutoHotKey Script and add to it this codes and save it then run script

PgDn::^#Right ;Next Desktop
return 

PgUp::^#Left ;Previous Desktop
return 

and it will work nice :)

2
  • 1
    did you even read the question before you double-posted?
    – jiggunjer
    Jan 17, 2017 at 2:47
  • yes 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 !!!!! Jan 17, 2017 at 3:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .