0

I'm looking for a autohotkey function, system shortcut or dll call to cycle between all windows on the current virtual desktop.

Windows 10 has this option (Pressing Alt+Tab shows windows that are open to only the desktop I'm using), but that is a global setting that will affect normal alt+tab use. I want to keep that as multi-desktop switch.

1

Try this:

F1:: WinMenuOnCurrentVirtualDesktop()

WinMenuOnCurrentVirtualDesktop(){
    list := ""
    Menu, windows, Add
    Menu, windows, deleteAll
    WinGet, id, list
    Loop, %id%
    {
        this_ID := id%A_Index%
        WinGetTitle, title, ahk_id %this_ID%
        If (title = "")
            continue            
        If !IsWindow(WinExist("ahk_id" . this_ID))
            continue
        If !IsWindowOnCurrentVirtualDesktop(this_ID)
            continue
        Menu, windows, Add, %title%, ActivateTitle      
        WinGet, Path, ProcessPath, ahk_id %this_ID%
        Try 
            Menu, windows, Icon, %title%, %Path%,, 0
        Catch 
            Menu, windows, Icon, %title%, %A_WinDir%\System32\SHELL32.dll, 3, 0 
    }
    CoordMode, Mouse, Screen
    MouseMove, (0.4*A_ScreenWidth), (0.35*A_ScreenHeight)
    CoordMode, Menu, Screen
    Xm := (0.25*A_ScreenWidth)
    Ym := (0.25*A_ScreenHeight)
    Menu, windows, Show, %Xm%, %Ym%
}

ActivateTitle:
    SetTitleMatchMode 3
    WinActivate, %A_ThisMenuItem%
return

;-----------------------------------------------------------------
; Check whether the target window is activation target
;-----------------------------------------------------------------

IsWindow(hwnd) {
    WinGet, s, Style, ahk_id %hwnd% 
    return s & 0xC00000 ? (s & 0x100 ? 0 : 1) : 0
} 

/* 
IsWindow(hWnd){
    WinGet, dwStyle, Style, ahk_id %hWnd%
    if ((dwStyle&0x08000000) || !(dwStyle&0x10000000)) {
        return false
    }
    WinGet, dwExStyle, ExStyle, ahk_id %hWnd%
    if (dwExStyle & 0x00000080) {
        return false
    }
    WinGetClass, szClass, ahk_id %hWnd%
    if (szClass = "TApplication") {
        return false
    }
    return true
}
*/


;--------------------------------------------------------------------------------
;Indicates whether the provided window is on the currently active virtual desktop.
;https://autohotkey.com/boards/viewtopic.php?p=64295#p64295
;--------------------------------------------------------------------------------
IsWindowOnCurrentVirtualDesktop(hWnd) {
    onCurrentDesktop := ""
    CLSID := "{aa509086-5ca9-4c25-8f95-589d3c07b48a}" 
    IID := "{a5cd92ff-29be-454c-8d04-d82879fb3f1b}"
    IVirtualDesktopManager := ComObjCreate(CLSID, IID)  
    Error := DllCall(NumGet(NumGet(IVirtualDesktopManager+0), 3*A_PtrSize), "Ptr", IVirtualDesktopManager, "Ptr", hWnd, "IntP", onCurrentDesktop)
    ObjRelease(IVirtualDesktopManager)  
    if !(Error=0)
        return false, ErrorLevel := true
    return onCurrentDesktop, ErrorLevel := false
}

EDIT:

Try also

F1:: WinMenuOnCurrentVirtualDesktop()

WinMenuOnCurrentVirtualDesktop(){
    Menu, Windows, Add
    Menu, Windows, deleteAll
    WinGet, id, list
    Loop %id%
    {
        this_ID := id%A_Index%
        WinGetTitle title, ahk_id %this_ID%
        If (title = "")
            continue
        ; If (title = "this title") ; add exceptions
            ; continue
        If !IsWindow(WinExist("ahk_id" . this_ID))
            continue
        If !IsWindowOnCurrentVirtualDesktop(WinExist("ahk_id" . this_ID))
            continue
        WinGetClass class, ahk_id %this_ID%
        ; If (class = "this class") ; add exceptions
            ; continue
        If (class = "ApplicationFrameWindow") 
        {
            WinGetText, text, ahk_id %this_ID%      
            If (text = "")
            {
                WinGet, style, style, ahk_id %this_ID%
                If !(style = "0xB4CF0000")   ; the window isn't minimized
                    continue
            }
        }
        WinGet, Path, ProcessPath, ahk_id %this_ID%
        Menu, Windows, Add, %title%, Activate_Window
        Try 
            Menu, Windows, Icon, %title%, %Path%,, 0
        Catch
            Menu, Windows, Icon, %title%, %A_WinDir%\System32\SHELL32.dll, 3, 0
    }
    CoordMode, Mouse, Screen
    MouseMove, (0.4*A_ScreenWidth), (0.35*A_ScreenHeight)
    CoordMode, Menu, Screen
    Xm := (0.25*A_ScreenWidth)
    Ym := (0.25*A_ScreenHeight)
    Menu, windows, Show, %Xm%, %Ym%
}

Activate_Window:
    SetTitleMatchMode, 3
    WinGetClass, Class, %A_ThisMenuItem%
    If (Class="Windows.UI.Core.CoreWindow") ; the minimized window has another class 
        WinActivate, %A_ThisMenuItem% ahk_class ApplicationFrameWindow
    else
        WinActivate, %A_ThisMenuItem%
return

;-----------------------------------------------------------------
; Check whether the target window is activation target
;-----------------------------------------------------------------

IsWindow(hwnd) {
    WinGet, s, Style, ahk_id %hwnd% 
    return s & 0xC00000 ? (s & 0x100 ? 0 : 1) : 0
} 


;--------------------------------------------------------------------------------
;Indicates whether the provided window is on the currently active virtual desktop.
;https://autohotkey.com/boards/viewtopic.php?p=64295#p64295
;--------------------------------------------------------------------------------
IsWindowOnCurrentVirtualDesktop(hWnd) {
    onCurrentDesktop := ""
    CLSID := "{aa509086-5ca9-4c25-8f95-589d3c07b48a}" 
    IID := "{a5cd92ff-29be-454c-8d04-d82879fb3f1b}"
    IVirtualDesktopManager := ComObjCreate(CLSID, IID)  
    Error := DllCall(NumGet(NumGet(IVirtualDesktopManager+0), 3*A_PtrSize), "Ptr", IVirtualDesktopManager, "Ptr", hWnd, "IntP", onCurrentDesktop)
    ObjRelease(IVirtualDesktopManager)  
    if !(Error=0)
        return false, ErrorLevel := true
    return onCurrentDesktop, ErrorLevel := false
}
9
  • This indeed finds all windows in the current virtual destop (with two unwanted ones - Microsoft Text Input Application and Microsoft Store - probably can filter them out). Thanks, accepting answer. – Krzysztof Bociurko Jul 10 '19 at 14:51
  • After windows update, the list of applications is larger than just the two above. This includes Calculator, Videos, Pictures, Microsoft Teams. And the Store changes name, so it can't be filtered like this. Must find better way to filter, as this method is not so correct as it was. – Krzysztof Bociurko Aug 2 '19 at 11:40
  • 1
    Try the edited IsWindow(hwnd) function. – user3419297 Aug 2 '19 at 12:07
  • 1
  • 1
    I don't have any problems with it (Build 18362.295). Try also the edited answer. – user3419297 Aug 22 '19 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.