pmb6tz / windows-desktop-switcher

An AutoHotKey script for Windows that lets a user change virtual desktops by pressing CapsLock + <num>.
MIT License
1.21k stars 229 forks source link

Move (only) current window to the destination desktop without switching #98

Open llinfeng opened 8 months ago

llinfeng commented 8 months ago

Does the DLL call for MoveWindowToDesktopNumberProc automatically activate the destination desktop as well? According to the ReadMe, the switch to the destination desktop seems to be implied in the following explanation:

Move the current window to another desktop, then switch to it

After inspecting how the MoveCurrentWindowToDesktop function in this repo is defined, I tried two versions of SendCurrentWindowToDesktop as detailed below:

; as defined
MoveCurrentWindowToDesktop(desktopNumber) {
    WinGet, activeHwnd, ID, A
    DllCall(MoveWindowToDesktopNumberProc, UInt, activeHwnd, UInt, desktopNumber - 1)
    switchDesktopByNumber(desktopNumber)
}

; V1 - not working - without switchDesktopByNumber, I'm still sent to the destination desktop
SendCurrentWindowToDesktopWithoutSwitching(desktopNumber) {
    WinGet, activeHwnd, ID, A
    DllCall(MoveWindowToDesktopNumberProc, UInt, activeHwnd, UInt, desktopNumber - 1)
}

; V2 - with notable flashes due to the wait
SendCurrentWindowToDesktopWithoutSwitching(desktopNumber) {
    currentDesktopNumber := GetCurrentDesktopNumber()
    WinGet, activeHwnd, ID, A
    DllCall(MoveWindowToDesktopNumberProc, UInt, activeHwnd, UInt, desktopNumber - 1)
    sleep 70  ; this wait is to make sure we can switch back to where we came from
              ; a smaller number may not work reliably.
    switchDesktopByNumber(currentDesktopNumber)
}

Here, Version 1 of SendCurrentWindowToDesktopWithoutSwitching performs both sending and switching actions. In contrast, Version 2 first sends the window, switches to the destination desktop, and then returns to the original desktop.

This SendCurrentWindowToDesktopWithoutSwitching function can be useful for sending multiple windows from the same desktop, to one or more destinations as one sees fit.


Or, am I encountering something more interesting, such as the auto-switch-to-destination action being specific to my AHK configuration?