question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Focus when switching to desktop

See original GitHub issue

To reproduce:

  1. Create two virtual desktops, open notepad on each.
  2. On the first desktop, click the notepad window to bring it to the foreground
  3. Use the windows short cut “ctrl+winkey+right arrow key” to go to second desktop
  4. Use Get-Desktop 0 | Switch-Desktop to go back to first virtual desktop

Focus isn’t restored to the notepad window. Let me know if you are able to reduce this, it is somewhat intermittent.

Issue Analytics

  • State:open
  • Created 5 months ago
  • Comments:16 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
MScholtescommented, May 14, 2023

Hello,

I think I have a working solution.

After reading a lot about this, especially in the issue at mzomparelli (who should no longer be supported “openly”, as he makes closed software), @FuPeiJiang in combination with @rotemgrim’s comment gave me a solution: before switching the desktop, the focus should be placed on a window that is available on each virtual desktop, so that the focus is simply retained when switching! In VD.ahk, the taskbar (window class Shell_TrayWnd) is used for this, but I think the desktop is more useful. The idea to give away the focus with a message to minimize is a great idea (I don’t know who had it initially).

With the following code blocks (in C++) it worked for me:

HWND wnd = FindWindow(NULL, "Program Manager");
DWORD DesktopThreadId = GetWindowThreadProcessId(wnd, NULL);
DWORD ForegroundThreadId = GetWindowThreadProcessId(GetForegroundWindow(), NULL);
DWORD CurrentThreadId = GetCurrentThreadId();

if ((DesktopThreadId != 0) && (ForegroundThreadId != 0) && (ForegroundThreadId != CurrentThreadId))
{
  AttachThreadInput(DesktopThreadId, CurrentThreadId, true);
  AttachThreadInput(ForegroundThreadId, CurrentThreadId, true);
  SetForegroundWindow(wnd);
  AttachThreadInput(ForegroundThreadId, CurrentThreadId, false);
  AttachThreadInput(DesktopThreadId, CurrentThreadId, false);
}

. here is the code for switching the virtual desktop .

HWND wnd = FindWindow(NULL, "Program Manager");
ShowWindow(wnd, SW_MINIMIZE);

I’m not entirely happy with it because it’s probably not stable in every situation. I am still testing and experimenting…

Greetings

Markus

0reactions
MScholtescommented, Jul 8, 2023

Hello @rotemgrim,

this is the permanent solution. However, it may not work in all cases, as it requires a certain cooperation of the displayed windows. I hope, however, it will work in almost all cases.

Greetings

Markus

Read more comments on GitHub >

github_iconTop Results From Across the Web

Switching desktops doesn't focus front application.
My Mail app on desktop 1 will remain greyed out and unfocused, resulting in keyboard shortcuts triggering in Safari instead of Mail. I...
Read more >
Apps don't gain focus when switching between them in ...
I have this weird issue where if I switch between two apps with a trackpad they do not gain focus and I have...
Read more >
Windows HotKey to go (focus)to the Desktop without ...
For Windows 10, press alt+esc. It should unfocus all windows and focus on desktop.
Read more >
No application focused after switching desktop
As the title says, after switching desktops, no application is focused. This is extremely annoying, because additional ALT+TAB is required ...
Read more >
Why does the Focus assist auto turn on when I switch back ...
when I switch to desktop view, the Focus assist turns on automatically, then I switch to any apps in normal, it turns off....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found