Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can autohotkey detect which virtual desktop you're on?

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
like image 569
aappletart Avatar asked Oct 16 '25 04:10

aappletart


2 Answers

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
}
like image 179
Сергей Червонос Avatar answered Oct 19 '25 13:10

Сергей Червонос


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
like image 26
noelicus Avatar answered Oct 19 '25 12:10

noelicus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!