FuPeiJiang / VD.ahk

Windows Virtual Desktop, AutoHotkey, Windows 11 support, Windows Server 2022, switch desktop, move window(wintitle) to current desktop; createDesktop, PinWindow, getCount, getDesktopNumOfWindow -> mute all windows in Virtual Desktop
MIT License
319 stars 45 forks source link

Error #48

Closed TaroDumpling closed 12 months ago

TaroDumpling commented 1 year ago

I'm using _VD.ahk. My goal is to create a code when I activate a window with mouse left click, I can just go to the virtual desktop I desire, and mouse middle click on desktop, then it will bring the previously activated window to this new virtual desktop. But I'm not sure what went wrong, nothing happens when I middle-click on a different virtual desktop.

   #If MouseIsOver("ahk_class WorkerW")

   MButton::
      VD.MoveWindowToCurrentDesktop("A")
   Return()
   #If

   MouseIsOver(WinTitle) {
      MouseGetPos,,, Win
      return WinExist(WinTitle . " ahk_id " . Win)
   }
   Return

I tried the following code to see if other function works, but I got an error message.

F2::
VD.goToDesktopNum(2)
Return

Autohotkey_iyRqNK7wph

When I run the folloinwg code, it returns empty message box.

#Include %A_LineFile%\..\..\VD.ahk

foundProcesses := ""
; Make sure to get all windows from all virtual desktops
DetectHiddenWindows On
WinGet, id, List
Loop %id%
{
    hwnd := id%A_Index%
    ;VD.getDesktopNumOfWindow will filter out invalid windows
    desktopNum_ := VD.getDesktopNumOfWindow("ahk_id" hwnd)
    If (desktopNum_ > -1) ;-1 for invalid window, 0 for "Show on all desktops", 1 for Desktop 1
    {
        WinGet, exe, ProcessName, % "ahk_id" hwnd
        foundProcesses .= desktopNum_ " " exe "`n"
    }
}

MsgBox % foundProcesses
xav-ie commented 12 months ago

You need to check that C:\Ahk\Library\VD.ahk file exists first. It probably does not. You need to make sure you put repo in AutoHotkey folder or wherever you keep AutoHotkey scripts. Your code is not relevant yet until you first fix your sourcing issue

FuPeiJiang commented 12 months ago

copy SetForeGroundWindow.exe into C:\Ahk\Library\, it needs to be in the same folder as VD.ahk


When I run the folloinwg code, it returns empty message box.

what is your windows version ? what is the value of A_OSVersion ? :

MsgBox % Clipboard:=A_OSVersion

My goal is to create a code when I activate a window with mouse left click, I can just go to the virtual desktop I desire

what is the code for LButton ? you've only sent the code for MButton

LButton::
???
TaroDumpling commented 12 months ago
MsgBox % Clipboard:=A_OSVersion

I moved SetForeGroundWindow.exe to where VD.ahk is, same issue. I'm actually using _VD.ahk, but I renamed it as VD.ahk, could that be a problem? My version is 10.0.22621. As for LButton, there is no code for it. It's just when you click on an application, it activates that window. So I was thinking once I activated it, I can just go to another virtual desktop, and middle click on the desktop to bring that previously activated window to the new virtual desktop. Thanks for trying to help.

TaroDumpling commented 12 months ago

You need to check that C:\Ahk\Library\VD.ahk file exists first. It probably does not. You need to make sure you put repo in AutoHotkey folder or wherever you keep AutoHotkey scripts. Your code is not relevant yet until you first fix your sourcing issue

VD.ahk does exist in C:\Ahk\Library, and the scripts I have included previously have been working just fine. So I doubt that was the problem. But just in case, below is how I included my scripts:

#Include C:\Ahk\Library\
#Include Class_NvAPI.ahk
#Include FindText.ahk
#Include desktop_switcher.ahk
#Include RegionWaitChange.ahk
#Include VA.ahk
#Include VD.ahk
FuPeiJiang commented 12 months ago

I'm actually using _VD.ahk, but I renamed it as VD.ahk

then you need to add 1 line:

VD.init()

so it becomes (please don't rename it, keep it as _VD.ahk):

#Include %A_LineFile%\..\..\_VD.ahk
VD.init()

foundProcesses := ""
; Make sure to get all windows from all virtual desktops
DetectHiddenWindows On
WinGet, id, List
Loop %id%
{
    hwnd := id%A_Index%
    ;VD.getDesktopNumOfWindow will filter out invalid windows
    desktopNum_ := VD.getDesktopNumOfWindow("ahk_id" hwnd)
    If (desktopNum_ > -1) ;-1 for invalid window, 0 for "Show on all desktops", 1 for Desktop 1
    {
        WinGet, exe, ProcessName, % "ahk_id" hwnd
        foundProcesses .= desktopNum_ " " exe "`n"
    }
}

MsgBox % foundProcesses

if you #Include _VD.ahk, you have to call VD.init() if you #Include VD.ahk, it will #Include _VD.ahk and call VD.init() for you that's the difference between the 2 files.

TaroDumpling commented 12 months ago

I'm actually using _VD.ahk, but I renamed it as VD.ahk

then you need to add 1 line:

VD.init()

so it becomes (please don't rename it, keep it as _VD.ahk):

#Include %A_LineFile%\..\..\_VD.ahk
VD.init()

foundProcesses := ""
; Make sure to get all windows from all virtual desktops
DetectHiddenWindows On
WinGet, id, List
Loop %id%
{
    hwnd := id%A_Index%
    ;VD.getDesktopNumOfWindow will filter out invalid windows
    desktopNum_ := VD.getDesktopNumOfWindow("ahk_id" hwnd)
    If (desktopNum_ > -1) ;-1 for invalid window, 0 for "Show on all desktops", 1 for Desktop 1
    {
        WinGet, exe, ProcessName, % "ahk_id" hwnd
        foundProcesses .= desktopNum_ " " exe "`n"
    }
}

MsgBox % foundProcesses

if you #Include _VD.ahk, you have to call VD.init() if you #Include VD.ahk, it will #Include _VD.ahk and call VD.init() for you that's the difference between the 2 files.

Thank you so much, this fixed it!

FuPeiJiang commented 12 months ago

So I was thinking once I activated it, I can just go to another virtual desktop, and middle click on the desktop to bring that previously activated window to the new virtual desktop.

VD.MoveWindowToCurrentDesktop("A")

when you go to another virtual desktop, the active window changes to a window on the VD you go to, (the active windows is literally what you are seeing in front of you), so this will bring the current active window to the current desktop, doing nothing

so you will have to save the last active window in another VD somehow, (save the hwnd of that window) then:

VD.MoveWindowToCurrentDesktop("ahk_id " savedHwnd)

@TaroDumpling this works for the simple of cases, I need bug testing (it doesn't always activate, but I need to know the precise steps that make it not activate)

middle click bring other vd active window.ahk:

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance force
ListLines Off
SetBatchLines -1
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#KeyHistory 0
#WinActivateForce

Process, Priority,, H

#Include %A_LineFile%\..\..\VD.ahk

SetWinDelay -1
SetControlDelay -1

DllCall("RegisterShellHookWindow","Ptr",A_ScriptHwnd)
OnMessage(DllCall("RegisterWindowMessage",Str,"SHELLHOOK"), "ShellMessage")

VD.RegisterDesktopNotifications()
VD.CurrentVirtualDesktopChanged:=Func("CurrentVirtualDesktopChanged")
CurrentVirtualDesktopChanged(desktopNum_Old, desktopNum_New) {
    global other_VD_hwnd, current_VD_Hwnd
    other_VD_hwnd:=current_VD_Hwnd
    current_VD_Hwnd:=false
}

current_VD_Hwnd:=WinExist("A")

#If MouseIsOver("ahk_class Progman ahk_exe explorer.exe")

MButton::
if (other_VD_hwnd) {
    VD.MoveWindowToCurrentDesktop("ahk_id " other_VD_hwnd)
}
return

#If

MouseIsOver(WinTitle) {
    MouseGetPos,,, Win
    return WinExist(WinTitle) == Win
}

Return

ShellMessage(wParam,lParam) {
    global current_VD_Hwnd
    switch wParam {
        case 2, 4, 32772:
            ; WinGetTitle, title, ahk_id %lParam%
            ; ToolTip WinActivated`n%Title%
            if (lParam) {
                current_VD_Hwnd:=lParam
            }
        ; default:
            ; ToolTip % wParam
    }
}

f3::Exitapp
TaroDumpling commented 12 months ago

So I was thinking once I activated it, I can just go to another virtual desktop, and middle click on the desktop to bring that previously activated window to the new virtual desktop.

VD.MoveWindowToCurrentDesktop("A")

when you go to another virtual desktop, the active window changes to a window on the VD you go to, (the active windows is literally what you are seeing in front of you), so this will bring the current active window to the current desktop, doing nothing

so you will have to save the last active window in another VD somehow, (save the hwnd of that window) then:

VD.MoveWindowToCurrentDesktop("ahk_id " savedHwnd)

@TaroDumpling this works for the simple of cases, I need bug testing (it doesn't always activate, but I need to know the precise steps that make it not activate)

middle click bring other vd active window.ahk:

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#SingleInstance force
ListLines Off
SetBatchLines -1
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#KeyHistory 0
#WinActivateForce

Process, Priority,, H

#Include %A_LineFile%\..\..\VD.ahk

SetWinDelay -1
SetControlDelay -1

DllCall("RegisterShellHookWindow","Ptr",A_ScriptHwnd)
OnMessage(DllCall("RegisterWindowMessage",Str,"SHELLHOOK"), "ShellMessage")

VD.RegisterDesktopNotifications()
VD.CurrentVirtualDesktopChanged:=Func("CurrentVirtualDesktopChanged")
CurrentVirtualDesktopChanged(desktopNum_Old, desktopNum_New) {
    global other_VD_hwnd, current_VD_Hwnd
    other_VD_hwnd:=current_VD_Hwnd
    current_VD_Hwnd:=false
}

current_VD_Hwnd:=WinExist("A")

#If MouseIsOver("ahk_class Progman ahk_exe explorer.exe")

MButton::
if (other_VD_hwnd) {
    VD.MoveWindowToCurrentDesktop("ahk_id " other_VD_hwnd)
}
return

#If

MouseIsOver(WinTitle) {
    MouseGetPos,,, Win
    return WinExist(WinTitle) == Win
}

Return

ShellMessage(wParam,lParam) {
    global current_VD_Hwnd
    switch wParam {
        case 2, 4, 32772:
            ; WinGetTitle, title, ahk_id %lParam%
            ; ToolTip WinActivated`n%Title%
            if (lParam) {
                current_VD_Hwnd:=lParam
            }
        ; default:
            ; ToolTip % wParam
    }
}

f3::Exitapp

I figured out an easier and more practical solution. I want to do the same thing using hotkey 1 through 9. Are there ways to make my code shorter instead of write the code for hotkey 1, hotkey 2, hotkey 3, and so forth? I wanted to not send numbers when I'm long pressing, one downside of my code is that I'm able to notice latency with typing numbers. Thanks!

;  Move Window to Desktop by Long Pressing number 1 to 9.
      *1::
         KeyWait, 1, T0.5
         If ErrorLevel = 1
         {
            Soundbeep, 1500
            VD.MoveWindowToDesktopNum("A", 1)
            KeyWait, 1
         }
         Else Send {1}
      Return

      *2::
         KeyWait, 2, T0.5
         If ErrorLevel = 1
         {
            Soundbeep, 1500
            VD.MoveWindowToDesktopNum("A", 2)
            KeyWait, 2
         }
         Else Send {2}
      Return
FuPeiJiang commented 12 months ago

I can think of 2 ways:

*1::
*2::
*3::
*4::
*5::
*6::
*7::
*8::
*9::
    numeric:=SubStr(A_ThisHotkey,2,1)
    KeyWait % numeric, T0.5
    If ErrorLevel = 1
    {
    Soundbeep, 1500
    VD.MoveWindowToDesktopNum("A", numeric)
    KeyWait % numeric
    }
    Else Send % numeric
Return
*1::foo("1")
*2::foo("2")
*3::foo("3")
*4::foo("4")
*5::foo("5")
*6::foo("6")
*7::foo("7")
*8::foo("8")
*9::foo("9")
foo(numeric) {
    KeyWait % numeric, T0.5
    If ErrorLevel = 1
    {
    Soundbeep, 1500
    VD.MoveWindowToDesktopNum("A", numeric)
    KeyWait % numeric
    }
    Else Send % numeric
}

one downside of my code is that I'm able to notice latency with typing numbers.

numbers are sent on KeyUp, instead of the default: on KeyDown

if any other key is pressed while a number is down(like when typing), you might want to send the number immediately, but I don't immediately know how to code that

TaroDumpling commented 12 months ago

I can think of 2 ways:

*1::
*2::
*3::
*4::
*5::
*6::
*7::
*8::
*9::
    numeric:=SubStr(A_ThisHotkey,2,1)
    KeyWait % numeric, T0.5
    If ErrorLevel = 1
    {
    Soundbeep, 1500
    VD.MoveWindowToDesktopNum("A", numeric)
    KeyWait % numeric
    }
    Else Send % numeric
Return
*1::foo("1")
*2::foo("2")
*3::foo("3")
*4::foo("4")
*5::foo("5")
*6::foo("6")
*7::foo("7")
*8::foo("8")
*9::foo("9")
foo(numeric) {
    KeyWait % numeric, T0.5
    If ErrorLevel = 1
    {
    Soundbeep, 1500
    VD.MoveWindowToDesktopNum("A", numeric)
    KeyWait % numeric
    }
    Else Send % numeric
}

one downside of my code is that I'm able to notice latency with typing numbers.

numbers are sent on KeyUp, instead of the default: on KeyDown

if any other key is pressed while a number is down(like when typing), you might want to send the number immediately, but I don't immediately know how to code that

I really appreciate the advice, thank you so much!