Question Detect user focus and type of control anywhere in the system

ShavedJesus

New member
Joined
Mar 17, 2021
Messages
4
Programming Experience
3-5
Hello all,

I'm developing a background application which detects keys pressed by the user and perform some operations accordingly.

I already developed the low-level part which detects keys pression. What I need is any tip to understand, anywhere at OS level (so also outside of my application), which window is focused (I think something like this should do the job:

C#:
IntPtr foregroundWindow = GetForegroundWindow();
uint foregroundProcess = GetWindowThreadProcessId(foregroundWindow, IntPtr.Zero);
)
but, mostly, to understand if the user is focusing a control where text can be inserted.

Given example:

my application is in the background. The user opens Google Chrome and starts typing a URL in the address bar. At this point, I do need to understand that the focus is on Goggle Chrome application and the user is typing text in a textbox.

Is it possible to make this? If yes, I'm not asking a full solution (even if would be good to have), but just a starting point because I tried to search but unsuccessful.

Thanks a lot in advance.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,122
Location
Chesapeake, VA
Programming Experience
10+
Say what? You're developing a keylogger?
 

ShavedJesus

New member
Joined
Mar 17, 2021
Messages
4
Programming Experience
3-5
Hello,

thanks for your reply.
It could seem, but I don't have to log anything, the purpose is totally different.
What I want to achieve is to have a sort of autocompletion software while typing, like the one you have on a smartphone.

Thanks.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,122
Location
Chesapeake, VA
Programming Experience
10+
The main problem you'll run into is determining if the user is typing into a text box. Back in the Win3.x days this would have been relatively easy because there not that many custom controls back then and you could simply check to see if the current control that has the focus has a class name of "EDIT". If you fastforward to now, everybody has their own custom controls. Not everyone will be using a plain of edit box. You'll have to detect the window class of the current control to see if it is one of the many that may act like an edit box. But that's only a quarter of the problem for the Windows applications that are using the classic Win32 window controls. If the application is one giant painting canvas and everything is painted within it, including it's edit boxes, then you won't be able to do the detection. Simple example: A DirectX game that has it's own chat window -- how will you find out that the user is typing within that chat window?
 

ShavedJesus

New member
Joined
Mar 17, 2021
Messages
4
Programming Experience
3-5
Hello Skydiver.
Thanks a lot for the explanation, I can understand this and that's a pity.
What do you think if I assume that if the user types 2-3 characters (including letters, numbers and symbols) they're typing text?
But a problem I see is that could happen the customner thinks for example he has the focus on the textbox he needs but he doesn't, so it wouldn't make sense to process the characters.

Otherwise I would need to handle only the standard "textboxes/controls".
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
3,122
Location
Chesapeake, VA
Programming Experience
10+
Try running spyxx.exe and spyxx_amd64.exe and dragging the Window Search tool over Chrome, Edge, and Firefox. See if you can even get any child controls. I only have the first two, and both of them have just one giant window as a canvas. Try the same with any modern WPF application. I think you'll also find a giant window canvas as well. I think that you'll find that the utility of your autocomplete utility is going to be severely limited.
 
Top Bottom