VirtualDesktop icon indicating copy to clipboard operation
VirtualDesktop copied to clipboard

Cannot activate window after switching desktop

Open Norhua opened this issue 5 months ago • 6 comments

When switching desktops using VirtualDesktop11-24H2.exe, the windows on the target desktop are not activated in some cases after the switch. This behavior differs from switching desktops by directly clicking the Task View icon on the taskbar or using the Win+Ctrl keyboard shortcut. A video demonstration is provided below. One notable detail is that after switching desktops, the window appears to be activated momentarily—I noticed the text cursor blinking briefly—but then immediately reverts to an inactive state. https://github.com/user-attachments/assets/fac6100c-210c-41c4-afc4-1c3370e86026

To clarify why I mentioned "in some cases": Yesterday when preparing to submit this issue, I tested the behavior and to my surprise, the window activation worked correctly. However, today when repeating the exact same test procedure, it fails to activate the window.

My system version information is as follows: Edition Windows 11 Pro Version 24H2 Installed on ‎7/‎14/‎2025 OS build 26100.4652 Experience Windows Feature Experience Pack 1000.26100.128.0

Looking forward to your response. Thank you!

Norhua avatar Jul 18 '25 04:07 Norhua

Hello @Norhua,

Micosoft seems to change the behaviour of the Explorer Desktop slightly with 24H2, activating the desktop background and giving away the focus does not work anymore (see the hard way to get it https://github.com/MScholtes/VirtualDesktop/issues/57). I reworked the code to activate the correct window after switching. This seems to work with animated switching (/anim:1), with direct switching (/anim:0) sometimes there is a flashing icon in the task bar. Maybe I will get even this fixed, for the moment I would ask you to test.

I uploaded a new version of VirtualDesktop11-24H2.cs for this. Can you check if it helps?

Greetings

Markus

MScholtes avatar Jul 22 '25 14:07 MScholtes

Hello @Norhua,

Micosoft seems to change the behaviour of the Explorer Desktop slightly with 24H2, activating the desktop background and giving away the focus does not work anymore (see the hard way to get it #57). I reworked the code to activate the correct window after switching. This seems to work with animated switching (/anim:1), with direct switching (/anim:0) sometimes there is a flashing icon in the task bar. Maybe I will get even this fixed, for the moment I would ask you to test.

I uploaded a new version of VirtualDesktop11-24H2.cs for this. Can you check if it helps?

Greetings

Markus

Thanks for the reply. I attempted to fix the issue where switching desktops fails to activate the target window when using the latest VirtualDesktop11-24H2.cs with animation enabled, but I was unsuccessful. However, I observed that after switching desktops, the Windows logo (Start menu icon) gains focus. A video demonstration is provided below

https://github.com/user-attachments/assets/d5c6fb91-bf3e-4129-ab56-34aba002f92f

Norhua avatar Jul 24 '25 09:07 Norhua

Hello @Norhua,

I can't replicate the behaviour you describe in my environment. But then again, I don't use Visual Studio Code.

Since Virtualdesktop doesn't run in the background, it can't maintain a window activation list. The procedure I implemented is to switch the desktop, activate the task list there, and then send the taskbar a message to go into the background.

I have no idea for a better algorithm I can implement in a "One Shot" application.

Greetings

Markus

MScholtes avatar Jul 25 '25 17:07 MScholtes

Hello @Norhua,

I can't replicate the behaviour you describe in my environment. But then again, I don't use Visual Studio Code.

Since Virtualdesktop doesn't run in the background, it can't maintain a window activation list. The procedure I implemented is to switch the desktop, activate the task list there, and then send the taskbar a message to go into the background.

I have no idea for a better algorithm I can implement in a "One Shot" application.

Greetings

Markus

Thank you for your reply. As a programming beginner with no prior experience in C#, I only use VSCode for convenient code viewing. After reading your response, I'm still unclear about how you utilize VirtualDesktop for window switching. Personally, I use AHK (AutoHotkey) to bind related operations.

Regarding the window activation issue, I believe we don't need to track the window states across virtual desktops. Instead, after switching desktops, we can simply activate the topmost window in the Z-order on the current virtual desktop. Since it was the activated window before switching, it should naturally be at the top of the Z-order. Of course, real-world implementation might require excluding certain windows.

Currently, I'm attempting to activate windows using AHK with: top := DllCall("GetTopWindow", "ptr", 0, "ptr") However, this isn't working ideally because GetTopWindow appears unable to distinguish between virtual desktops—it sometimes returns windows from other desktops.

Norhua avatar Jul 26 '25 06:07 Norhua

Hello @Norhua,

asking Windows for the active window is like a cat chasing its own tail: you're asking for the active window to activate it? Why? It is already activated.

Here is a good implementation for AutoHotkey: https://github.com/FuPeiJiang/VD.ahk

Greetings

Markus

MScholtes avatar Aug 12 '25 16:08 MScholtes

Hello @Norhua,

asking Windows for the active window is like a cat chasing its own tail: you're asking for the active window to activate it? Why? It is already activated.

Here is a good implementation for AutoHotkey: https://github.com/FuPeiJiang/VD.ahk

Greetings

Markus

Thank you very much, this AHK script meets my needs perfectly!

Norhua avatar Aug 19 '25 07:08 Norhua