I recently switched to Windows 10, and have found that one really annoying habit it has is that it will randomly switch focus without warning. I can be typing an email, scrolling down through the errors in the event viewer to see what vital parts of Windows crashed today and so on, and suddenly I find that my words are not entered into the email, the events aren’t scrolling, etc.
A quick bit of searching revealed that a lot of people have had the same problem, and very few have managed to solve it. The main problem is finding out what gets the focus.
This issue is very hard to diagnose, partly because it only happens when you are least expecting it. Sometimes it can happen several times in a short space of time, then when you are waiting for it, it doesn’t happen. Putting aside the temptation to think that this is some clever piece of AI that Microsoft included in Windows to annoy you (not beyond the realms of fantasy, given how many other annoying features are in Windows already, and how reluctant Microsoft seem to be to even acknowledge that people might find them annoying), it does seem like a bug (gasp, you don’t mean…).
Being an incurable tinkerer, I decided to see if I could find out what was going on. The Windows API includes a function to get the foreground window, as well as one to get the text of the current window. Armed with some help from a now-extinct blog post (archive can be found here), I came up with the following, rather simple, but effective program…
using System; using System.Text; using System.Runtime.InteropServices; using System.Timers; namespace WhatHasFocus { class Program { [DllImport("user32.dll")] static extern int GetForegroundWindow(); [DllImport("user32.dll")] static extern int GetWindowText(int hWnd, StringBuilder text, int count); static void Main(string[] args) { Timer timer = new Timer(1000); timer.Elapsed += GetActiveWindow; timer.AutoReset = true; timer.Enabled = true; Console.ReadLine(); } private static string lastTitle = ""; private static int lastHandle; private static void GetActiveWindow(object _, ElapsedEventArgs __) { const int nChars = 256; StringBuilder sb = new StringBuilder(nChars); int handle = GetForegroundWindow(); if (GetWindowText(handle, sb, nChars) > 0) { if (handle != lastHandle || sb.ToString() != lastTitle) { lastHandle = handle; lastTitle = sb.ToString(); Console.WriteLine($"{DateTime.Now.ToLongTimeString()} ({handle}) {sb}"); } } else { if (handle != lastHandle || sb.ToString() != lastTitle) { Console.WriteLine($"{DateTime.Now.ToLongTimeString()} (none)"); lastHandle = handle; lastTitle = ""; } } } } }
Although the code above ran fine for me, Diana reported (see comments below) a compiler error ‘Timer’ is an ambiguous reference between ‘System.Timers.Timer’ and ‘System.Threading.Timer’. This can be fixed by fully qualifying the declaration as
System.Timers.Timer timer = new System.Timers.Timer(1000);
This is just a console application project, using .NET Framework (not Core, although it would probably look the same if you used Core). It checks the current foreground window every second, and it has changed since the last time it reported, it dumps a line into the console window. If there isn’t a current foreground window (say if you click on the desktop), it prints “(none)”. I suspect that’s what I’ll see when the issue next happens.
When you run this, you get a console window that opens, which you can leave running in the background. When your window loses focus, you look in the console window and see what got it. In theory, this should give you a clue as to what’s going on…
In practice, I strongly suspect it won’t help, but it was fun to write!
Predictably, since I set it running, I haven’t encountered the issue. Maybe that’s the answer!
If you aren’t interested in the code, and just want to see what is grabbing focus, then you might be better off using FocusLogger, which does basically the same as this code, but has a better UI. Thanks to Diana for pointing that out.
Not being a programmer, I don’t know how to compile this so I can run it. How do I go about that?
Thanks.
You’d need to have Visual Studio installed, and create a new console application.
Then copy the code into your main class and run it. Should just work!
This is amazing! My games kept loosing focus and I couldn’t figure out what was taking it!
You’re welcome! Glad it was useful.
Hey, thanks for this, crazy how it’s not more convenient to check any other way than writing a console command yourself but that’s the reality we’re in I guess. If anything, it’s an excuse to tinker and work on a fun project, can’t say it wasn’t interesting for me too.
As Tim M, I too was plagued by my games being unfocused by something that wasn’t showing up in task bar or any other place, turns out it was Razer Synapse (profile being unsynced)
Only thing I had to change was
System.Timers.Timer timer = new System.Timers.Timer(1000);
because I was being given the error
‘Timer’ is an ambiguous reference between ‘System.Timers.Timer’ and ‘System.Threading.Timer’
to anyone else being new to visual studio:
.NET desktop development needs to be installed on the Visual Studio installer.
On the Create a new project page, enter console in the search box. Next, choose C#. Choose the Console App template, and then choose Next.
can delete the things that are there and copy paste the code block from the post, as well as change the timer error as I’ve written out above (if you need to).
A bit after writing this out there is also a program you can search by googling JocysCom/FocusLogger over on github that may also work, but haven’t tried it myself
As for Razer Synapse itself, I dug around and found this reply https://insider.razer.com/razer-support-45/razer-synapse-3-causing-fullscreen-games-to-minimise-without-warning-43939?postid=173588#post173588
“You need to uninstall Razer Synapse. I used Revo uninstaller to make sure I removed everything.
Then restart your PC.
Once restarted, Down load synapse again. Install it.
Once installed, open synapse, click on the little Cog/gear icon on the top right.
Uncheck Notifications.”
Which I’ve done, hopefully to stop this from happening again.
Hey Diana, thanks for the reply.
I seem to remember getting mixed up with those two timers. Really not sure why Microsoft added two subtly different timers in different namespaces, and gave them the same name! Not sure why you got the error and I didn’t. Maybe I forgot to include a “using” in the code I posted. Anyway, glad you got sorted, and thanks for clarifying the point. I’ll update the post.
JocysCom/FocusLogger looks good. I did think about doing mine as a Windows app, but wanted something quick and easy, so concentrated on the functionality rather than the UI. Could easily turn it into an app, but I haven’t used it years, so no point in repeating what someone else has done.
Thanks again
Avrohom Yisroel
From how people are talking here, it sounds like “”focus” means something different than just whatever window you are currently working in, but I’m not sure what else it would logically mean.
How can you see what window or application or whatever has “focus”?
Not sure what you mean. Focus has a fairly well-defined meaning, and I don’t see anyone sounding confused about it.
Maybe if you explain what confuses you, I can explain.