-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdesktop-switcher.ahk
99 lines (88 loc) · 3.95 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
;
; AutoHotkey v2
;
ProcessSetPriority("Realtime", "AutoHotkey64.exe")
DetectHiddenWindows(True)
hVirtualDesktopAccessor := DllCall("LoadLibrary", "Str", "VirtualDesktopAccessor.dll", "Ptr")
GoToDesktopNumberProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "GoToDesktopNumber", "Ptr")
GetCurrentDesktopNumberProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "GetCurrentDesktopNumber", "Ptr")
GetDesktopCountProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "GetDesktopCount", "Ptr")
IsWindowOnCurrentVirtualDesktopProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "IsWindowOnCurrentVirtualDesktop", "Ptr")
MoveWindowToDesktopNumberProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "MoveWindowToDesktopNumber", "Ptr")
RegisterPostMessageHookProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "RegisterPostMessageHook", "Ptr")
UnregisterPostMessageHookProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "UnregisterPostMessageHook", "Ptr")
IsPinnedWindowProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "IsPinnedWindow", "Ptr")
RestartVirtualDesktopAccessorProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "RestartVirtualDesktopAccessor", "Ptr")
GetWindowDesktopNumberProc := DllCall("GetProcAddress", "Ptr", hVirtualDesktopAccessor, "AStr", "GetWindowDesktopNumber", "Ptr")
activeWindowByDesktop := Map()
monitorWidth := SysGet(0)
monitorHeight := SysGet(1)
GoToDesktopNumber(num) {
activeHwnd := WinGetID("A")
currentDesktop := DllCall(GetCurrentDesktopNumberProc, "UInt")
isPinned := DllCall(IsPinnedWindowProc, "UInt", activeHwnd)
if (isPinned == 0) {
activeWindowByDesktop[currentDesktop] := activeHwnd
}
; Try to avoid flashing task bar buttons, deactivate the current window if it is not pinned
if (isPinned != 1 and WinExist("ahk_class Shell_TrayWnd")) {
WinActivate("ahk_class Shell_TrayWnd")
}
DllCall(GoToDesktopNumberProc, "Int", num)
; Try to restore active window from memory (if it's still on the desktop and is not pinned)
activeHwnd := WinGetID("A")
isPinned := DllCall(IsPinnedWindowProc, "UInt", activeHwnd)
if (activeWindowByDesktop.Has(num)) {
oldHwnd := activeWindowByDesktop[num]
isOnDesktop := DllCall(IsWindowOnCurrentVirtualDesktopProc, "UInt", oldHwnd, "Int")
if (isOnDesktop == 1 && isPinned != 1) {
WinActivate("ahk_id " . oldHwnd)
activeWindowByDesktop.Delete(num)
}
}
CoordMode("ToolTip")
ToolTip(" " . num+1 . " ", monitorWidth/2, monitorHeight/2)
SetTimer(() => ToolTip(), -500)
}
GoToNextDesktop() {
currentDesktop := DllCall(GetCurrentDesktopNumberProc, "UInt")
lastDesktop := DllCall(GetDesktopCountProc, "UInt") - 1
if (currentDesktop = lastDesktop) {
GoToDesktopNumber(0)
} else {
GoToDesktopNumber(currentDesktop + 1)
}
}
GoToPrevDesktop() {
currentDesktop := DllCall(GetCurrentDesktopNumberProc, "UInt")
lastDesktop := DllCall(GetDesktopCountProc, "UInt") - 1
if (currentDesktop = 0) {
GoToDesktopNumber(lastDesktop)
} else {
GoToDesktopNumber(currentDesktop - 1)
}
}
; Middle mouse button click on left or right screen part
MButton:: {
CoordMode("Mouse", "Screen")
MouseGetPos(&xpos, &ypos)
if (xpos > monitorWidth/2) {
GoToNextDesktop()
;Send("#^{Right}")
;Sleep(100)
;Send("#^{Right}")
} else {
GoToPrevDesktop()
;Send("#^{Left}")
}
}
;Ctrl+number switch desktop
!1::GoToDesktopNumber(0)
!2::GoToDesktopNumber(1)
!3::GoToDesktopNumber(2)
!4::GoToDesktopNumber(3)
!5::GoToDesktopNumber(4)
!6::GoToDesktopNumber(5)
!7::GoToDesktopNumber(6)
!8::GoToDesktopNumber(7)
!9::GoToDesktopNumber(8)