Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
yo3yo3yo3yo3 opened this issue Nov 16, 2021 · 4 comments

Comments

@yo3yo3yo3yo3
Copy link

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.

@beliakou
Copy link

beliakou commented Jan 2, 2022

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

    Send !{Esc}

line to focusTheForemostWindow function

@FuPeiJiang
Copy link

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

vnavkal pushed a commit to vnavkal/windows-desktop-switcher that referenced this issue Jan 18, 2022
@dalianzhu
Copy link

dalianzhu commented Mar 20, 2022

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.

@geekyBoogiepop
Copy link

geekyBoogiepop commented May 23, 2022

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?

imawizard added a commit to imawizard/MiguruWM that referenced this issue Feb 1, 2023
Use built-in hotkey Alt-Escape instead of manually invoking WinActivate.
Credits: pmb6tz/windows-desktop-switcher#77 (comment)
Restoring the windows focus is actually necessary because of the
WinActivate(Shell_TrayWnd) which in turn fixes the taskbar flashing.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants