-
-
Notifications
You must be signed in to change notification settings - Fork 233
/
desktop_switcher.ahk
236 lines (204 loc) · 8.3 KB
/
desktop_switcher.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#Requires AutoHotkey v1.1.33+
#SingleInstance Force ; The script will Reload if launched while already running
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases
#KeyHistory 0 ; Ensures user privacy when debugging is not needed
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory
SendMode Input ; Recommended for new scripts due to its superior speed and reliability
; Globals
DesktopCount := 2 ; Windows starts with 2 desktops at boot
CurrentDesktop := 1 ; Desktop count is 1-indexed (Microsoft numbers them this way)
LastOpenedDesktop := 1
; DLL
hVirtualDesktopAccessor := DllCall("LoadLibrary", "Str", A_ScriptDir . "\VirtualDesktopAccessor.dll", "Ptr")
global IsWindowOnDesktopNumberProc := DllCall("GetProcAddress", Ptr, hVirtualDesktopAccessor, AStr, "IsWindowOnDesktopNumber", "Ptr")
global MoveWindowToDesktopNumberProc := DllCall("GetProcAddress", Ptr, hVirtualDesktopAccessor, AStr, "MoveWindowToDesktopNumber", "Ptr")
global GoToDesktopNumberProc := DllCall("GetProcAddress", Ptr, hVirtualDesktopAccessor, AStr, "GoToDesktopNumber", "Ptr")
; Main
SetKeyDelay, 75
mapDesktopsFromRegistry()
OutputDebug, [loading] desktops: %DesktopCount% current: %CurrentDesktop%
#Include %A_ScriptDir%\user_config.ahk
return
;
; This function examines the registry to build an accurate list of the current virtual desktops and which one we're currently on.
; List of desktops appears to be in HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops
; On Windows 11 the current desktop UUID appears to be in the same location
; On previous versions in HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SessionInfo\1\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\VirtualDesktops, CurrentVirtualDesktop
if ErrorLevel {
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 := floor(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 functions finds out ID of current session.
;
getSessionId()
{
ProcessId := DllCall("GetCurrentProcessId", "UInt")
if ErrorLevel {
OutputDebug, Error getting current process id: %ErrorLevel%
return
}
OutputDebug, Current Process Id: %ProcessId%
DllCall("ProcessIdToSessionId", "UInt", ProcessId, "UInt*", SessionId)
if ErrorLevel {
OutputDebug, Error getting session id: %ErrorLevel%
return
}
OutputDebug, Current Session Id: %SessionId%
return SessionId
}
_switchDesktopToTarget(targetDesktop)
{
; Globals variables should have been updated via updateGlobalVariables() prior to entering this function
global CurrentDesktop, DesktopCount, LastOpenedDesktop
; Don't attempt to switch to an invalid desktop
if (targetDesktop > DesktopCount || targetDesktop < 1 || targetDesktop == CurrentDesktop) {
OutputDebug, [invalid] target: %targetDesktop% current: %CurrentDesktop%
return
}
LastOpenedDesktop := CurrentDesktop
; Fixes the issue of active windows in intermediate desktops capturing the switch shortcut and therefore delaying or stopping the switching sequence. This also fixes the flashing window button after switching in the taskbar. More info: https://github.com/pmb6tz/windows-desktop-switcher/pull/19
WinActivate, ahk_class Shell_TrayWnd
DllCall(GoToDesktopNumberProc, Int, targetDesktop-1)
; Makes the WinActivate fix less intrusive
Sleep, 50
focusTheForemostWindow(targetDesktop)
}
updateGlobalVariables()
{
; 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()
}
switchDesktopByNumber(targetDesktop)
{
global CurrentDesktop, DesktopCount
updateGlobalVariables()
_switchDesktopToTarget(targetDesktop)
}
switchDesktopToLastOpened()
{
global CurrentDesktop, DesktopCount, LastOpenedDesktop
updateGlobalVariables()
_switchDesktopToTarget(LastOpenedDesktop)
}
switchDesktopToRight()
{
global CurrentDesktop, DesktopCount
updateGlobalVariables()
_switchDesktopToTarget(CurrentDesktop == DesktopCount ? 1 : CurrentDesktop + 1)
}
switchDesktopToLeft()
{
global CurrentDesktop, DesktopCount
updateGlobalVariables()
_switchDesktopToTarget(CurrentDesktop == 1 ? DesktopCount : CurrentDesktop - 1)
}
focusTheForemostWindow(targetDesktop) {
foremostWindowId := getForemostWindowIdOnDesktop(targetDesktop)
if isWindowNonMinimized(foremostWindowId) {
WinActivate, ahk_id %foremostWindowId%
}
}
isWindowNonMinimized(windowId) {
WinGet MMX, MinMax, ahk_id %windowId%
return MMX != -1
}
getForemostWindowIdOnDesktop(n)
{
n := n - 1 ; Desktops start at 0, while in script it's 1
; winIDList contains a list of windows IDs ordered from the top to the bottom for each desktop.
WinGet winIDList, list
Loop % winIDList {
windowID := % winIDList%A_Index%
windowIsOnDesktop := DllCall(IsWindowOnDesktopNumberProc, UInt, windowID, UInt, n)
; Select the first (and foremost) window which is in the specified desktop.
if (windowIsOnDesktop == 1) {
return windowID
}
}
}
MoveCurrentWindowToDesktop(desktopNumber) {
WinGet, activeHwnd, ID, A
DllCall(MoveWindowToDesktopNumberProc, UInt, activeHwnd, UInt, desktopNumber - 1)
switchDesktopByNumber(desktopNumber)
}
MoveCurrentWindowToRightDesktop()
{
global CurrentDesktop, DesktopCount
updateGlobalVariables()
WinGet, activeHwnd, ID, A
DllCall(MoveWindowToDesktopNumberProc, UInt, activeHwnd, UInt, (CurrentDesktop == DesktopCount ? 1 : CurrentDesktop + 1) - 1)
_switchDesktopToTarget(CurrentDesktop == DesktopCount ? 1 : CurrentDesktop + 1)
}
MoveCurrentWindowToLeftDesktop()
{
global CurrentDesktop, DesktopCount
updateGlobalVariables()
WinGet, activeHwnd, ID, A
DllCall(MoveWindowToDesktopNumberProc, UInt, activeHwnd, UInt, (CurrentDesktop == 1 ? DesktopCount : CurrentDesktop - 1) - 1)
_switchDesktopToTarget(CurrentDesktop == 1 ? DesktopCount : CurrentDesktop - 1)
}
;
; 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, LastOpenedDesktop
Send, #^{F4}
if (LastOpenedDesktop >= CurrentDesktop) {
LastOpenedDesktop--
}
DesktopCount--
CurrentDesktop--
OutputDebug, [delete] desktops: %DesktopCount% current: %CurrentDesktop%
}