Switch between windows on only one virtual desktop with Autohotkey - with alt+tab working as-is

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.

Krzysztof Bociurko

Posted 2019-07-08T10:28:37.860

Reputation: 209

Answers

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
}

user3419297

Posted 2019-07-08T10:28:37.860

Reputation: 2 055

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 – 2019-07-10T14:51:07.737

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 – 2019-08-02T11:40:52.317

1Try the edited IsWindow(hwnd) function. – user3419297 – 2019-08-02T12:07:00.417

Eope, the five iconless unwanted UWP apps are still there: https://i.imgur.com/A7a3XgR.png - none of them were turned on by me since reset, I don't think i willingly used the store or movies app this year. In the task manager, they are not in the Apps list, but are in the background processes tab. If I open and close them, some (all except Edge) will disappear from both your list and background processes.

– Krzysztof Bociurko – 2019-08-05T07:58:41.263

1

See How to stop apps from running in the background on Windows 10

– user3419297 – 2019-08-05T11:14:53.153

Used this tip for some time, but still it doesn't solve all problems. For example, Microsoft Teams that is running in the background and has a tray icon is not alt-tabbable, but is selectable with this method (from any desktop). Not to mention, I had to disable like 30 apps, and every new app has to manually disabled there. – Krzysztof Bociurko – 2019-08-22T14:36:13.927

1I don't have any problems with it (Build 18362.295). Try also the edited answer. – user3419297 – 2019-08-22T15:43:43.380

Windows build 1903 18362.267, Microsoft Teams 1.2.00.21068. Just noticed that Teams has also other problems: for example it can have a visible window with focus, but not have an entry in the taskbar. After closing and opening the window again (not restarting, it's still in the tray), it starts behaving normally - for a time. – Krzysztof Bociurko – 2019-08-23T08:33:34.060

Try to repair, reset or re-install this App after making a system backup. – user3419297 – 2019-08-23T14:27:27.103