pmb6tz/windows-desktop-switcher

Window does not auto-focus to foreground upon changing virtual desktops

yo3yo3yo3yo3 opened this issue · 4 comments

If I'm on Desktop 1 and press alt+2 to go to Desktop 2, the full-screen app on Desktop 2 is not in "focus" in the foreground and requires clicking the window or pressing alt+tab to bring the window into focus. I only have 1 app running on every Virtual Desktop, so I would just like that window to automatically be called to the foreground as soon as I switch to that Desktop so that I can simply use a key command to do something like focus the URL bar and begin searching the web. Currently, when I switch Desktops using the keyboard shortcut, I have to press alt+tab or click the window every time in order to bring focus to the window and begin typing in it.

In researching the issue, it sounds like someone has an idea to fix it that has been given a couple of "thumbs ups" to indicate it works, but I'm not a coder and don't really know how to implement it. Could this be inserted into the existing AHK script somehow?

"I was able to resolve this by calling AllowSetForegroundWindow(ASFW_ANY) before calling GoToDesktopNumber(). Presumably AutoHotKey does this, but I had to do it explicitly." I found that at the bottom of this github post.

In absence of other solutions I was able to fix this by adding

    Send !{Esc}

line to focusTheForemostWindow function

another solution (by mzomparelli)
in autohotkey: https://github.com/FuPeiJiang/VD.ahk/blob/e74cfaa4a15a044a0ba58106b26b5927364947df/VD.ahk#L349-L356

    _SwitchDesktop(IVirtualDesktop) {
        ;activate taskbar before ;https://github.com/mzomparelli/zVirtualDesktop/issues/59#issuecomment-317613971
        WinActivate, ahk_class Shell_TrayWnd
        WinWaitActive, ahk_class Shell_TrayWnd
        this._dll_SwitchDesktop(IVirtualDesktop)
        this._dll_SwitchDesktop(IVirtualDesktop)
        WinMinimize, ahk_class Shell_TrayWnd
    }

I Don't know how WinMinimize does its magic, but it's like !{Esc}


equivalent function:

!2::VD.goToDesktopNum(2)

I don't know what AllowSetForegroundWindow(ASFW_ANY) is
but after some googling : Window does not auto-focus to foreground upon changing virtual desktops

DllCall("User32\AllowSetForegroundWindow", "Int",-1) ;ASFW_ANY is -1 ;https://www.reddit.com/r/AutoHotkey/comments/qvkjhh/comment/hkx42s7/?utm_source=share&utm_medium=web2x&context=3

In absence of other solutions I was able to fix this by adding

    Send !{Esc}

line to focusTheForemostWindow function

Add this code in the end of if block,this works. thanks.

In absence of other solutions I was able to fix this by adding

    Send !{Esc}

line to focusTheForemostWindow function

Thank you so much. I had a really hard time working without this fix for a couple of weeks. Should I make a pull request for this?