使用方法:
Capslock + 数字 –> 切换桌面
Capslock + Shift + 数字 –> 把当前窗口带到某桌面
Switch to desktop] OR [Move the current window to the X-th desktop
源码:
;Capslock + 数字 --> 切换桌面
; Capslock + Shift + 数字 --> 把当前窗口带到某桌面
; [Switch to desktop] OR [Move the current window to the X-th desktop]
CapsLock & 1::
if GetKeyState("Shift")
MoveActiveWindowToDesktop(1)
else SwitchToDesktop(1)
return
CapsLock & 2::
if GetKeyState("Shift")
MoveActiveWindowToDesktop(2)
else SwitchToDesktop(2)
return
CapsLock & 3::
if GetKeyState("Shift")
MoveActiveWindowToDesktop(3)
else SwitchToDesktop(3)
return
CapsLock & 4::
if GetKeyState("Shift")
MoveActiveWindowToDesktop(4)
else SwitchToDesktop(4)
return
CapsLock & 5::
if GetKeyState("Shift")
MoveActiveWindowToDesktop(5)
else SwitchToDesktop(5)
return
CapsLock & 6::
if GetKeyState("Shift")
MoveActiveWindowToDesktop(6)
else SwitchToDesktop(6)
return
CapsLock & 7::
if GetKeyState("Shift")
MoveActiveWindowToDesktop(7)
else SwitchToDesktop(7)
return
CapsLock & 8::
if GetKeyState("Shift")
MoveActiveWindowToDesktop(8)
else SwitchToDesktop(8)
return
CapsLock & 9::
if GetKeyState("Shift")
MoveActiveWindowToDesktop(9)
else SwitchToDesktop(9)
return
CapsLock & 0::
if GetKeyState("Shift")
MoveActiveWindowToDesktop(10)
else SwitchToDesktop(1)
return
; Capslock & -::
;If GetKeyState("Shift")
;MoveActiveWindowToDesktop(11)
;else SwitchToDesktop(1)
;Return
; Capslock & =::
;If GetKeyState("Shift")
;MoveActiveWindowToDesktop(12)
;else SwitchToDesktop(1)
;Return
SwitchToDesktop(idx){
if (!SwitchToDesktopByInternalAPI(idx)){
TrayTip , WARN, SwitchToDesktopByHotkey
SwitchToDesktopByHotkey(idx)
}
}
SwitchToDesktopByHotkey(idx){
SendInput ^#{Left 10}
idx -= 1
loop %idx% {
SendInput ^#{Right}
}
}
SwitchToDesktopByInternalAPI(idx){
succ := 0
IServiceProvider := ComObjCreate("{C2F03A33-21F5-47FA-B4BB-156362A2F239}", "{6D5140C1-7436-11CE-8034-00AA006009FA}")
IVirtualDesktopManagerInternal := ComObjQuery(IServiceProvider, "{C5E0CDCA-7B6E-41B2-9FC4-D93975CC467B}", "{F31574D6-B682-4CDC-BD56-1827860ABEC6}")
ObjRelease(IServiceProvider)
if (IVirtualDesktopManagerInternal){
GetCount := vtable(IVirtualDesktopManagerInternal, 3)
GetDesktops := vtable(IVirtualDesktopManagerInternal, 7)
SwitchDesktop := vtable(IVirtualDesktopManagerInternal, 9)
; TrayTip, , % IVirtualDesktopManagerInternal
pDesktopIObjectArray := 0
DllCall(GetDesktops, "Ptr", IVirtualDesktopManagerInternal, "Ptr*", pDesktopIObjectArray)
if (pDesktopIObjectArray){
GetDesktopCount := vtable(pDesktopIObjectArray, 3)
GetDesktopAt := vtable(pDesktopIObjectArray, 4)
DllCall(GetDesktopCount, "Ptr", IVirtualDesktopManagerInternal, "UInt*", DesktopCount)
; if idx-th desktop doesn't exists then create a new desktop
if (idx > DesktopCount){
diff := idx - DesktopCount
loop %diff% {
Send ^#d
}
succ := 1
}
GetGuiDFromString(IID_IVirtualDesktop, "{FF72FFDD-BE7E-43FC-9C03-AD81681E88E4}")
DllCall(GetDesktopAt, "Ptr", pDesktopIObjectArray, "UInt", idx - 1, "Ptr", &IID_IVirtualDesktop, "Ptr*", VirtualDesktop)
ObjRelease(pDesktopIObjectArray)
if (VirtualDesktop){
DllCall(SwitchDesktop, "Ptr", IVirtualDesktopManagerInternal, "Ptr", VirtualDesktop)
ObjRelease(VirtualDesktop)
succ := 1
}
}
ObjRelease(IVirtualDesktopManagerInternal)
}
return succ
}
vtable(ptr, n){
; NumGet(ptr+0) Returns the address of the object's virtual function
; table (vtable for short). The remainder of the expression retrieves
; the address of the nth function's address from the vtable.
return NumGet(NumGet(ptr+0), n*A_PtrSize)
}
GetGUIDFromString(ByRef GUID, sGUID) ; Converts a string to a binary GUID
{
VarSetCapacity(GuiD, 16, 0)
DllCall("ole32CLSIDFromString", "Str", sGuiD, "Ptr", &GuiD)
}
MoveActiveWindowToDesktop(idx){
activeWin := WinActive("A")
WinHide ahk_id %activeWin%
SwitchToDesktop(idx)
WinShow ahk_id %activeWin%
WinActivate ahk_id %activeWin%
}
谢谢分享