Advertisement
Guest User

Untitled

a guest
Jan 18th, 2022
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.98 KB | None | 0 0
  1. ; Globals
  2. DesktopCount = 5 ; Windows starts with 2 desktops at boot
  3. CurrentDesktop = 1 ; Desktop count is 1-indexed (Microsoft numbers them this way)
  4. ;
  5. ; This function examines the registry to build an accurate list of the current virtual desktops and which one we're currently on.
  6. ; Current desktop UUID appears to be in HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SessionInfo\1\VirtualDesktops
  7. ; List of desktops appears to be in HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops
  8. ;
  9. mapDesktopsFromRegistry() {
  10. global CurrentDesktop, DesktopCount
  11. ; 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.
  12. IdLength := 32
  13. SessionId := getSessionId()
  14. if (SessionId) {
  15. RegRead, CurrentDesktopId, HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SessionInfo\%SessionId%\VirtualDesktops, CurrentVirtualDesktop
  16. if (CurrentDesktopId) {
  17. IdLength := StrLen(CurrentDesktopId)
  18. }
  19. }
  20. ; Get a list of the UUIDs for all virtual desktops on the system
  21. RegRead, DesktopList, HKEY_CURRENT_USER, SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VirtualDesktops, VirtualDesktopIDs
  22. if (DesktopList) {
  23. DesktopListLength := StrLen(DesktopList)
  24. ; Figure out how many virtual desktops there are
  25. DesktopCount := DesktopListLength / IdLength
  26. }
  27. else {
  28. DesktopCount := 1
  29. }
  30. ; Parse the REG_DATA string that stores the array of UUID's for virtual desktops in the registry.
  31. i := 0
  32. while (CurrentDesktopId and i < DesktopCount) {
  33. StartPos := (i * IdLength) + 1
  34. DesktopIter := SubStr(DesktopList, StartPos, IdLength)
  35. OutputDebug, The iterator is pointing at %DesktopIter% and count is %i%.
  36. ; Break out if we find a match in the list. If we didn't find anything, keep the
  37. ; old guess and pray we're still correct :-D.
  38. if (DesktopIter = CurrentDesktopId) {
  39. CurrentDesktop := i + 1
  40. OutputDebug, Current desktop number is %CurrentDesktop% with an ID of %DesktopIter%.
  41. break
  42. }
  43. i++
  44. }
  45. }
  46. ;
  47. ; This functions finds out ID of current session.
  48. ;
  49. getSessionId()
  50. {
  51. ProcessId := DllCall("GetCurrentProcessId", "UInt")
  52. if ErrorLevel {
  53. OutputDebug, Error getting current process id: %ErrorLevel%
  54. return
  55. }
  56. OutputDebug, Current Process Id: %ProcessId%
  57. DllCall("ProcessIdToSessionId", "UInt", ProcessId, "UInt*", SessionId)
  58. if ErrorLevel {
  59. OutputDebug, Error getting session id: %ErrorLevel%
  60. return
  61. }
  62. OutputDebug, Current Session Id: %SessionId%
  63. return SessionId
  64. }
  65. ;
  66. ; This function switches to the desktop number provided.
  67. ;
  68. switchDesktopByNumber(targetDesktop)
  69. {
  70. global CurrentDesktop, DesktopCount
  71. ; Re-generate the list of desktops and where we fit in that. We do this because
  72. ; the user may have switched desktops via some other means than the script.
  73. mapDesktopsFromRegistry()
  74. ; Don't attempt to switch to an invalid desktop
  75. if (targetDesktop > DesktopCount || targetDesktop < 1) {
  76. OutputDebug, [invalid] target: %targetDesktop% current: %CurrentDesktop%
  77. return
  78. }
  79. ; Go right until we reach the desktop we want
  80. while(CurrentDesktop < targetDesktop) {
  81. Send ^#{Right}
  82. CurrentDesktop++
  83. OutputDebug, [right] target: %targetDesktop% current: %CurrentDesktop%
  84. }
  85. ; Go left until we reach the desktop we want
  86. while(CurrentDesktop > targetDesktop) {
  87. Send ^#{Left}
  88. CurrentDesktop--
  89. OutputDebug, [left] target: %targetDesktop% current: %CurrentDesktop%
  90. }
  91. }
  92. ;
  93. ; This function creates a new virtual desktop and switches to it
  94. ;
  95. createVirtualDesktop()
  96. {
  97. global CurrentDesktop, DesktopCount
  98. Send, #^d
  99. DesktopCount++
  100. CurrentDesktop = %DesktopCount%
  101. OutputDebug, [create] desktops: %DesktopCount% current: %CurrentDesktop%
  102. }
  103. ;
  104. ; This function deletes the current virtual desktop
  105. ;
  106. deleteVirtualDesktop()
  107. {
  108. global CurrentDesktop, DesktopCount
  109. Send, #^{F4}
  110. DesktopCount--
  111. CurrentDesktop--
  112. OutputDebug, [delete] desktops: %DesktopCount% current: %CurrentDesktop%
  113. }
  114. ; Main
  115. SetKeyDelay, 75
  116. mapDesktopsFromRegistry()
  117. OutputDebug, [loading] desktops: %DesktopCount% current: %CurrentDesktop%
  118. ; User config!
  119. ; This section binds the key combo to the switch/create/delete actions
  120. LAlt & s::switchDesktopByNumber(1)
  121. LAlt & w::switchDesktopByNumber(2)
  122. LAlt & e::switchDesktopByNumber(3)
  123. LAlt & d::switchDesktopByNumber(4)
  124. LAlt & f::switchDesktopByNumber(5)
  125.  
  126. ; LWin & 1::switchDesktopByNumber(1)
  127. ; LWin & 2::switchDesktopByNumber(2)
  128. ; LWin & 3::switchDesktopByNumber(3)
  129. ; LWin & 4::switchDesktopByNumber(4)
  130. ; LWin & 5::switchDesktopByNumber(5)
  131. ; LWin & 6::switchDesktopByNumber(6)
  132. ; LWin & 7::switchDesktopByNumber(7)
  133. ; LWin & 8::switchDesktopByNumber(8)
  134. ; LWin & 9::switchDesktopByNumber(9)
  135. ;CapsLock & 1::switchDesktopByNumber(1)
  136. ;CapsLock & 2::switchDesktopByNumber(2)
  137. ;CapsLock & 3::switchDesktopByNumber(3)
  138. ;CapsLock & 4::switchDesktopByNumber(4)
  139. ;CapsLock & 5::switchDesktopByNumber(5)
  140. ;CapsLock & 6::switchDesktopByNumber(6)
  141. ;CapsLock & 7::switchDesktopByNumber(7)
  142. ;CapsLock & 8::switchDesktopByNumber(8)
  143. ;CapsLock & 9::switchDesktopByNumber(9)
  144. ;CapsLock & n::switchDesktopByNumber(CurrentDesktop + 1)
  145. ;CapsLock & p::switchDesktopByNumber(CurrentDesktop - 1)
  146. ;CapsLock & s::switchDesktopByNumber(CurrentDesktop + 1)
  147. ;CapsLock & a::switchDesktopByNumber(CurrentDesktop - 1)
  148. ;CapsLock & c::createVirtualDesktop()
  149. ;CapsLock & d::deleteVirtualDesktop()
  150. ; Alternate keys for this config. Adding these because DragonFly (python) doesn't send CapsLock correctly.
  151. ;^!1::switchDesktopByNumber(1)
  152. ;^!2::switchDesktopByNumber(2)
  153. ;^!3::switchDesktopByNumber(3)
  154. ;^!4::switchDesktopByNumber(4)
  155. ;^!5::switchDesktopByNumber(5)
  156. ;^!6::switchDesktopByNumber(6)
  157. ;^!7::switchDesktopByNumber(7)
  158. ;^!8::switchDesktopByNumber(8)
  159. ;^!9::switchDesktopByNumber(9)
  160. ;^!n::switchDesktopByNumber(CurrentDesktop + 1)
  161. ;^!p::switchDesktopByNumber(CurrentDesktop - 1)
  162. ;^!s::switchDesktopByNumber(CurrentDesktop + 1)
  163. ;^!a::switchDesktopByNumber(CurrentDesktop - 1)
  164. ;^!c::createVirtualDesktop()
  165. ;^!d::deleteVirtualDesktop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement