Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent Windows from switching virtual desktop #43

Open
mmaukii opened this issue May 30, 2023 · 0 comments
Open

prevent Windows from switching virtual desktop #43

mmaukii opened this issue May 30, 2023 · 0 comments

Comments

@mmaukii
Copy link

mmaukii commented May 30, 2023

thanks for your code again!
When i am working with virtual desktop i really hate it, when i swith to an other program, which is opened on a allready existing desktop. Windows automatically changes the desktop to the called program and gives no notifaication about that. I prefer that an allready opened programm gets changed to the orignal one. With your code i found a hacky solution for that:

i add the last two lines in the function goToDesktopNum to write the with ahk choosen programm to an ini file.

goToDesktopNum(desktopNum) { ; Lej77 Grabacr07/VirtualDesktop#23 (comment)

    Gui VD_active_gui:New, % "-Border -SysMenu +Owner -Caption +HwndVD_active_gui_hwnd"
    DllCall("ShowWindow","Ptr",VD_active_gui_hwnd,"Int",1) ;you can only Show gui that's in another VD if a gui of same owned/process is already active

    this._WinActivateForceForceForce(VD_active_gui_hwnd) ;specifically for Teams.exe

    firstWindowId:=this._getFirstWindowInVD(desktopNum)

    Gui VD_animation_gui:New, % "-Border -SysMenu +Owner -Caption +HwndVD_animation_gui_hwnd"
    IVirtualDesktop := this._GetDesktops_Obj().GetAt(desktopNum)
    GetId:=this._vtable(IVirtualDesktop, 4)
    VarSetCapacity(GUID_Desktop, 16)
    DllCall(GetId, "Ptr", IVirtualDesktop, "Ptr", &GUID_Desktop)
    DllCall(this.MoveWindowToDesktop, "Ptr", this.IVirtualDesktopManager, "Ptr", VD_animation_gui_hwnd, "Ptr", &GUID_Desktop)
    DllCall("ShowWindow","Ptr",VD_animation_gui_hwnd,"Int",1) ;after gui on current desktop owned by current process became active window, Show gui on different desktop owned by current process
    ; loop 20 {
    ;     if (this.getCurrentDesktopNum()==desktopNum) { ; wildest hack ever..
    ;         if (firstWindowId) {
    ;             DllCall("SetForegroundWindow","Ptr",firstWindowId)
    ;         } else {
    ;             this._activateDesktopBackground()
    ;         }
    ;         Gui VD_animation_gui:Destroy
    ;         Gui VD_active_gui:Destroy
    ;         break
    ;     }
    ;     Sleep 25
    ; }
    if (firstWindowId) {
        DllCall("SetForegroundWindow","Ptr",firstWindowId)
    } else {
        this._activateDesktopBackground()
    }
    Gui VD_animation_gui:Destroy
    Gui VD_active_gui:Destroy
	choosenDesktop :=  desktopNum
	IniWrite, %choosenDesktop%, var.ini, variables, var1
}

then i have the following adapted script to check if the choosen and the actual desktop correspond. If not take the active Window an go back to the choosen desktop. Only drawback is, that you now are only able to change desktops with your ahk-code, what is for me ok. On other drawback is, that the following code has to run in an own instance. At least i did not find to run it together with the rest of the code in one script.

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance force
ListLines Off
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
SetBatchLines -1
#KeyHistory 0
;verhindert den automatischen Wechsel auf anderen desktop wenn Programm nur dort liegt

#Persistent
#Include VD.ahk
VD.RegisterDesktopNotifications()
VD.CurrentVirtualDesktopChanged:=Func("CurrentVirtualDesktopChanged")
CurrentVirtualDesktopChanged(desktopNum_Old, desktopNum_New) {
sleep 200
IniRead, choosenDesktop, ../var.ini, variables, var1
ToolTip % desktopNum_Old "-> " desktopNum_New " soll " choosenDesktop
SetTimer, RemoveToolTip, -1000
if (desktopNum_New!=choosenDesktop){ ;is desktop selected dekstop
sleep 200
VD.MoveWindowToDesktopNum("A",choosenDesktop), VD.goToDesktopNum(choosenDesktop)
}
}
return

RemoveToolTip:
ToolTip
return

f3::Exitapp

i thought maybe this is of interest for you. Sorry for the programming skills, its hacky, but it is a solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant