How to access virtual desktops directly instead of task bar menu and control windows left, right key?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Raghava Doregowda
Posts: 144
Joined: 06 Nov 2022, 01:48

How to access virtual desktops directly instead of task bar menu and control windows left, right key?

Post by Raghava Doregowda » 10 Feb 2023, 01:38

Virtual desktops when coupled with commands like control click are fantastic for programs that run in the background with active AHK scripts. However I would wish AHK had a unique identifier for the virtual desktops just like winclass wintitle. Is there a way to do that? Is there a way like "#ifvirtualdesktop 1 exist" type of command? So that hotkeys would work only within one VD, and it could be a powerful multitasker for running apps in the background with AHK scripts. It would also decongest desktops.

gmoises
Posts: 75
Joined: 18 Nov 2017, 16:43

Re: How to access virtual desktops directly instead of task bar menu and control windows left, right key?

Post by gmoises » 10 Feb 2023, 09:40

I have been using this code for many years

Code: Select all

; https://www.computerhope.com/tips/tip224.htm
; https://autohotkey.com/boards/viewtopic.php?t=9224
; This function examines the registry to build an accurate list of the current virtual desktops and which one we're currently on.
; Current desktop UUID appears to be in HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SessionInfo\1\VirtualDesktops
; List of desktops appears to be in HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops
mapDesktopsFromRegistry() {
	global CurrentDesktop, DesktopCount

	; Get the current desktop UUID. Length should be 32 always,
	; but there's no guarantee this couldn't change in a later Windows release so we check.
	IdLength := 32
	SessionId := getSessionId()
	If (SessionId)
	{	RegRead, CurrentDesktopId, HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SessionInfo\%SessionId%\VirtualDesktops, CurrentVirtualDesktop
		If (CurrentDesktopId)
			IdLength := StrLen(CurrentDesktopId)
	}

	; Get a list of the UUIDs for all virtual desktops on the system
	RegRead, DesktopList, HKEY_CURRENT_USER, SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops, VirtualDesktopIDs
	If (DesktopList)
	{	DesktopListLength := StrLen(DesktopList)
		; Figure out how many virtual desktops there are
		DesktopCount := Format("{1:i}", DesktopListLength / IdLength)
	} Else
		DesktopCount := 1
	

	; Parse the REG_DATA string that stores the array of UUID's for virtual desktops in the registry.
	i := 0
	While (CurrentDesktopId And i < DesktopCount)
	{	StartPos := (i * IdLength) + 1
		DesktopIter := SubStr(DesktopList, StartPos, IdLength)
		OutputDebug, The iterator is pointing at %DesktopIter% and count is %i%.

		; Break out if we find a match in the list. If we didn't find anything, keep the
		; old guess and pray we're still correct :-D.
		If (DesktopIter = CurrentDesktopId)
		{	CurrentDesktop := i + 1
			OutputDebug, Current desktop number is %CurrentDesktop% with an ID of %DesktopIter%.
			Break
		}
		++i
	}
}

; This function switches to the desktop number provided.
switchDesktopByNumber(targetDesktop) {
	global CurrentDesktop, DesktopCount

	; Re-generate the list of desktops and where we fit in that. We do this because
	; the user may have switched desktops via some other means than the script.
	mapDesktopsFromRegistry()

	; Don't attempt to switch to an invalid desktop
	If (targetDesktop > DesktopCount)
		targetDesktop := DesktopCount
	If (targetDesktop < 1)
		targetDesktop := 1

	; Go right until we reach the desktop we want
	While(CurrentDesktop < targetDesktop)
	{	Send ^#{Right}
		++CurrentDesktop
		Sleep, 30																				; tiempo de pausa
		OutputDebug, [right] target: %targetDesktop% current: %CurrentDesktop%
	}

	; Go left until we reach the desktop we want
	While(CurrentDesktop > targetDesktop)
	{	Send ^#{Left}
		--CurrentDesktop
		Sleep, 30
		OutputDebug, [left] target: %targetDesktop% current: %CurrentDesktop%
	}
	Menu, Tray,	Icon, % A_ScriptDir . "\Iconos\ICO\" . targetDesktop . "a.ico"					; poner el ícono
}

; This function creates a new virtual desktop and switches to it
createVirtualDesktop() {
	global CurrentDesktop, DesktopCount
	Send, #^d
	++DesktopCount
	CurrentDesktop := DesktopCount
	OutputDebug, [create] desktops: %DesktopCount% current: %CurrentDesktop%
}

; This function deletes the current virtual desktop
deleteVirtualDesktop() {
	global CurrentDesktop, DesktopCount
	Send, #^{F4}
	--DesktopCount
	--CurrentDesktop
	OutputDebug, [delete] desktops: %DesktopCount% current: %CurrentDesktop%
}

Raghava Doregowda
Posts: 144
Joined: 06 Nov 2022, 01:48

Re: How to access virtual desktops directly instead of task bar menu and control windows left, right key?

Post by Raghava Doregowda » 11 Mar 2023, 07:26

gmoises wrote:
10 Feb 2023, 09:40
I have been using this code for many years

Code: Select all

; https://www.computerhope.com/tips/tip224.htm
; https://autohotkey.com/boards/viewtopic.php?t=9224
; This function examines the registry to build an accurate list of the current virtual desktops and which one we're currently on.
; Current desktop UUID appears to be in HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SessionInfo\1\VirtualDesktops
; List of desktops appears to be in HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops
mapDesktopsFromRegistry() {
	global CurrentDesktop, DesktopCount

	; Get the current desktop UUID. Length should be 32 always,
	; but there's no guarantee this couldn't change in a later Windows release so we check.
	IdLength := 32
	SessionId := getSessionId()
	If (SessionId)
	{	RegRead, CurrentDesktopId, HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SessionInfo\%SessionId%\VirtualDesktops, CurrentVirtualDesktop
		If (CurrentDesktopId)
			IdLength := StrLen(CurrentDesktopId)
	}

	; Get a list of the UUIDs for all virtual desktops on the system
	RegRead, DesktopList, HKEY_CURRENT_USER, SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops, VirtualDesktopIDs
	If (DesktopList)
	{	DesktopListLength := StrLen(DesktopList)
		; Figure out how many virtual desktops there are
		DesktopCount := Format("{1:i}", DesktopListLength / IdLength)
	} Else
		DesktopCount := 1
	

	; Parse the REG_DATA string that stores the array of UUID's for virtual desktops in the registry.
	i := 0
	While (CurrentDesktopId And i < DesktopCount)
	{	StartPos := (i * IdLength) + 1
		DesktopIter := SubStr(DesktopList, StartPos, IdLength)
		OutputDebug, The iterator is pointing at %DesktopIter% and count is %i%.

		; Break out if we find a match in the list. If we didn't find anything, keep the
		; old guess and pray we're still correct :-D.
		If (DesktopIter = CurrentDesktopId)
		{	CurrentDesktop := i + 1
			OutputDebug, Current desktop number is %CurrentDesktop% with an ID of %DesktopIter%.
			Break
		}
		++i
	}
}

; This function switches to the desktop number provided.
switchDesktopByNumber(targetDesktop) {
	global CurrentDesktop, DesktopCount

	; Re-generate the list of desktops and where we fit in that. We do this because
	; the user may have switched desktops via some other means than the script.
	mapDesktopsFromRegistry()

	; Don't attempt to switch to an invalid desktop
	If (targetDesktop > DesktopCount)
		targetDesktop := DesktopCount
	If (targetDesktop < 1)
		targetDesktop := 1

	; Go right until we reach the desktop we want
	While(CurrentDesktop < targetDesktop)
	{	Send ^#{Right}
		++CurrentDesktop
		Sleep, 30																				; tiempo de pausa
		OutputDebug, [right] target: %targetDesktop% current: %CurrentDesktop%
	}

	; Go left until we reach the desktop we want
	While(CurrentDesktop > targetDesktop)
	{	Send ^#{Left}
		--CurrentDesktop
		Sleep, 30
		OutputDebug, [left] target: %targetDesktop% current: %CurrentDesktop%
	}
	Menu, Tray,	Icon, % A_ScriptDir . "\Iconos\ICO\" . targetDesktop . "a.ico"					; poner el ícono
}

; This function creates a new virtual desktop and switches to it
createVirtualDesktop() {
	global CurrentDesktop, DesktopCount
	Send, #^d
	++DesktopCount
	CurrentDesktop := DesktopCount
	OutputDebug, [create] desktops: %DesktopCount% current: %CurrentDesktop%
}

; This function deletes the current virtual desktop
deleteVirtualDesktop() {
	global CurrentDesktop, DesktopCount
	Send, #^{F4}
	--DesktopCount
	--CurrentDesktop
	OutputDebug, [delete] desktops: %DesktopCount% current: %CurrentDesktop%
}
When I try to run the script, it says Error: Call to nonexistent function.
Specifically: getSessionId()

Post Reply

Return to “Ask for Help (v1)”