I'm trying to check if javaw.exe has focus, then execute certain code if it does.
Previously I had code which would look for the process ID of javaw.exe, then compare it to the process which currently had focus, which worked for awhile, but then I noticed when I had more than one javaw.exe process running, it would only work on one of those processes, while I need it to work when any javaw.exe process has focus.
Is there any way to do this?
You can determine this quite easily using the
GetForegroundWindow()
andGetWindowThreadProcessId()
WinAPI functions.First call
GetForegroundWindow
to get the window handle of the currently focused window, then callGetWindowThreadProcessId
in order to retrieve the process id of that window. Finally get it as aProcess
class instance by callingProcess.GetProcessById()
Usage example:
Hope this helps!