3

Hi I'm new to autohotkey (and programming in general) and I wanted to write a script that lets me conveniently switch to a specific desktop. For example, in my script Capslock+3 switches to desktop 3.

As you can see or if you try it out, it's not very robust. The script only knows a desktop number separate from the real one. For example, if you run the script while on desktop 4, the script still starts with the desktop set to 1 and you have to press Caps+4 then Caps+1 to set it in the right direction. And if there is a flashing window in another desktop and you click it, it switches to that desktop while the script still thinks you're in the previous one.

I've searched for ways autohotkey can detect which desktop you're on, but could not find any.

Can anyone give any tips on how to improve it? Thanks! :D

SetCapsLockState, AlwaysOff
desktop = 1

Switch(d)
{
    global

    ;Determine how far away the desired desktop is from current one
    press := (d-desktop)

    desktop = %d%

    ;Determine which direction to switch desktops and stop script if already on current desktop
    If press < 0
        direction = Left
    else if press > 0
        direction = Right
    else
        return

    press := Abs(press)

    Loop, %press%
    {
        SendInput, ^#{%direction%}
        Sleep, 75
    }

    return
}

CapsLock & 1::
    Switch(1)
return

CapsLock & 2::
    Switch(2)
return

CapsLock & 3::
    Switch(3)
return

CapsLock & 4::
    Switch(4)
return

;In case user switches desktop with traditional shortcuts

^#Left::
    SendInput ^#{Left}
    If desktop > 1
        desktop--
return

^#Right::
    SendInput ^#{Right}
    If desktop < 4
        desktop++
return
aappletart
  • 41
  • 4
  • 1
    You need to clarify your problem more. What issues are you detecting that classifies as not robust? What were your attempts at fixing what you are concerned with? You may also check out this link for improving your question, https://stackoverflow.com/help/how-to-ask – Joseph K. Dec 12 '17 at 18:21
  • @JosephK. Changed the title and added some clarification. Thanks! – aappletart Dec 12 '17 at 18:33
  • Try [Switch to virtual desktop](https://autohotkey.com/boards/viewtopic.php?p=91414#p91414) or [Virtual Desktop Enhancer](https://github.com/sdias/win-10-virtual-desktop-enhancer). – user3419297 Dec 13 '17 at 06:06
  • I use VirtuaWin. Yes, it works on Windows 10 if you run as administrator. – TinkerTenorSoftwareGuy Dec 21 '17 at 17:48

4 Answers4

8

source

RegRead, cur, HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SessionInfo\1\VirtualDesktops, CurrentVirtualDesktop
RegRead, all, HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops, VirtualDesktopIDs
ix := floor(InStr(all,cur) / strlen(cur))
msgbox current desktop index: %ix%

If SessionInfo keeps changing (on a multiuser environment etc) then add a couple of Windows API calls:

SessionId := getSessionId()
RegRead, CurrentDesktopId, HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SessionInfo\%SessionId%\VirtualDesktops, CurrentVirtualDesktop
msgbox % CurrentDesktopId 

getSessionId(){
    ProcessId := DllCall("GetCurrentProcessId", "UInt")
    if ErrorLevel return 1
    DllCall("ProcessIdToSessionId", "UInt", ProcessId, "UInt*", SessionId)
    if ErrorLevel return 1
    return SessionId
}
  • This was working at one stage but stopped after a reboot session moved to session\5 , then had to keep editing the script each time the reg path changed. – Brad Aug 31 '20 at 09:26
  • This answer needs more explanation. – randy May 13 '21 at 19:59
3

To get desktop name a bit of string re-arrangement is needed. Here's what I've got to ToolTip and display the desktop switch when performed by the standard windows hotkey of WinKey+Left/Right

ConvertIdToDesktopKey(id)
{
    arr := StrSplit(id)
    str := "{" . arr[7] . arr[8] . arr[5] . arr[6] . arr[3] . arr[4] . arr[1] . arr[2] . "-" .      arr[11] . arr[12] . arr[9] . arr[10] . "-" .        arr[15] . arr[16] . arr[13]  . arr[14] . "-" .      arr[17] . arr[18] . arr[19] . arr[20] . "-" . SubStr(id, -11) . "}"

    return str
}

GetSessionId(){
    ProcessId := DllCall("GetCurrentProcessId", "UInt")
    if ErrorLevel return 1
    DllCall("ProcessIdToSessionId", "UInt", ProcessId, "UInt*", SessionId)
    if ErrorLevel return 1
    return SessionId
}

RemoveToolTip:
ToolTip
Return

ToolTipCurrentDesktop:
    SessionId := GetSessionId()
    RegRead, cur, HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SessionInfo\%SessionId%\VirtualDesktops, CurrentVirtualDesktop
    key := ConvertIdToDesktopKey(cur)
    RegRead, name, HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops\Desktops\%key%, Name
    ToolTip, - Desktop -`n%name%
    SetTimer, RemoveToolTip, -1800
Return

; Forward the standard windows shortcut for desktop switch, and set a timer for flashing the destination desktop name
#^Left::
    Send #^{LEFT}
    SetTimer, ToolTipCurrentDesktop, -800
Return

#^Right::
    Send #^{RIGHT}
    SetTimer, ToolTipCurrentDesktop, -800
Return
noelicus
  • 14,468
  • 3
  • 92
  • 111
  • 1
    Great! I combined this with a **hotkey to move windows to another desktop** from https://superuser.com/a/1538134/308266 – MD004 Oct 19 '22 at 17:55
2

I've made an ahk function library,

  • Numpad1 to go to Desktop 1
  • Numpad2 to go to Desktop 2
  • Numpad3 to go to Desktop 3

if you still need to detect which desktop you're on,
it's VD_getCurrentDesktop()

https://github.com/FuPeiJiang/VD.ahk

run "VD examples.ahk" to use the library

Mr. Doge
  • 796
  • 5
  • 11
0

If you need to stay with your utility and can't use VirtuaWin, for example, then I would not try to create an "aware" script that "detects" the current desktop. I would store the current desktop in a variable based upon the hotkeys you use to switch desktops, as you have been trying.

I think the problem lies in your Left and Right supporting traditional shortcuts. If there is a way to disable those shortcuts, you would have an easier and more robust script to maintain.