使用 Autohotkey 在一个虚拟桌面上切换窗口 - alt+tab 按原样工作

使用 Autohotkey 在一个虚拟桌面上切换窗口 - alt+tab 按原样工作

我正在寻找一个自动热键功能、系统快捷方式或 dll 调用,以便在当前虚拟桌面上的所有窗口之间循环。

Windows 10 有此选项(按 Alt+Tab 键可显示仅对我正在使用的桌面打开的窗口),但这是一个全局设置,会影响正常的 alt+tab 使用。我想将其保留为多桌面开关。

答案1

尝试这个:

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
}

编辑:

尝试一下

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
}

相关内容