pmb6tz / windows-desktop-switcher

An AutoHotKey script for Windows that lets a user change virtual desktops by pressing CapsLock + <num>.
MIT License
1.21k stars 229 forks source link

Switching leaves window out of focus #62

Open itkind opened 4 years ago

itkind commented 4 years ago

Hi, I have a question. I am switching between desktops with <!1::switchDesktopByNumber(1) and <!2::switchDesktopByNumber(2) In dekstop 1 I am using a txt editor, in desktop 2 something else. When I switch from desktop 1 to 2 and then back to 1 I can't continue writing till I click on the txt editor. All windows are out of focus when returning to the desktop. This doesen't happen when I slide with 4 fingers on the trackpad to switch between the desktops. Can you help me out please?

Thank you

ghost commented 4 years ago

I have the Same Problem

itkind commented 4 years ago

The problem is solved with this script: https://www.computerhope.com/tips/tip224.htm

sromeroi commented 3 years ago

Also affected with the issue.

I just saw the reference above to the URL/script. I tested it and works... Is the fix of this issue being included in this project in a future release?

Thanks.

Aran-Fey commented 3 years ago

The script from computerhope isn't quite as up-to-date as the latest version here on github (and also it's a horribly formatted mess), so I took it upon myself to figure out what the difference between that script and this one is.

The "fix" is to remove these two blocks of code from the _switchDesktopToTarget function:

; Fixes the issue of active windows in intermediate desktops capturing the switch shortcut and therefore delaying or stopping the switching sequence. This also fixes the flashing window button after switching in the taskbar. More info: https://github.com/pmb6tz/windows-desktop-switcher/pull/19
WinActivate, ahk_class Shell_TrayWnd
; Makes the WinActivate fix less intrusive
Sleep, 50
focusTheForemostWindow(targetDesktop)

But as you can tell from the comment, that code exists for a reason. And clearly the code already tries to restore the window focus by calling the focusTheForemostWindow function. So... why isn't it working?

As far as I can tell, there's something wrong with the isWindowNonMinimized function. Removing the "is the window minimized" check from the focusTheForemostWindow function works for me:

focusTheForemostWindow(targetDesktop) {
    foremostWindowId := getForemostWindowIdOnDesktop(targetDesktop)
    WinActivate, ahk_id %foremostWindowId%
}

As a side effect, this also seems to have fixed my inability to move windows to other desktops.

bedware commented 3 years ago

Hi there! Maybe my solution isn't "true", but It worked for me. What do you think? Can I propose it?

focusTheForemostWindow(targetDesktop) {
    Send !{Tab}
}