How to change input focus to a browser?
I want a program to move around and fill in fields on a web page
displayed by a browser, for example Firefox.
The main program uses CreateProcess to start the Firefox browser.
Firefox pops up and fills the whole screen and has input focus (is ready
accept key and mouse input).
MY idea is then to use SendInput to fire input events at the browser from
the main process. To decide what inputs to fire I will need to activate (give
focus to) to the main process, so that I can click in dialogs or menus to
make the decision.
SendInput can only provide input to the active window that has focus, which means I need to make the browser active before firing with SendInput.
How can I do that?
[751 byte] By [
kresander] at [2007-11-20 11:11:06]

# 1 Re: How to change input focus to a browser?
Use function FindWindow to btain handle of the window you want to bring to focus/active, then use SetFocus.
Besides, you shall better create timer to wait untill process becomes active (if this is not issue ignore it).
Secondly I suggest you directly embed Gecko component Mozilla based on, obtain details from Mozilla developers center. Or may be use native webbrowser component, instead of creating process which may result in unexpected behavior as per regular updates in GUI as well as wxWdigets (library behind FireFox browser).
regards
# 2 Re: How to change input focus to a browser?
Many thanks Ali.
I have been struggling a lot with this one and in the end I had to use FindWindow too.
My first attempt was:
<code>
if ( CreateProcess ( "\\...Firefox.exe ... , &pi ) )
{
WaitForInputIdle ( pi.hProcess ); // to let process fire up its io
// however - the window is not visible at this point.
// It can take several seconds for the browser window to show
// so waitforinputidle is not enough for valid synch
Sleep ( using various values ) ;
GetGUIThreadInfo ( pi.dwThreadId , &g );
//g.hwndActive is supposed to hold the handle of the outermost
// browser window at this point but did not always do so,
//50% of the time it returned NULL
}
</code>
Several iterations later I tried:
<code>
if ( CreateProcess ( "Mozilla Firefox ... , &pi ) )
{
WaitForInputIdle ( pi.hProcess ); // to let process fire up its io
// waitforinputidle above is probably redundant
hbrowser = NULL ;
while ( hbrowser == NULL ) // safetvest to stop looping not shown
{
hbrowser = FindWindow ( 0 , "Mozilla Firefox" ) ;
Sleep ( 1000 ) ;
} ;
// hbrowser now contains a valid outermost browser window
// GetGUIThreadInfo is not needed, but when I called it here
// it worked most of the time.
}
</code>
My plan was then to force the main process to the foreground and let the user choose modes and so on and then force the focus and foreground back to the browser. I used functions GetCurrentThreadId(), GetCurrentThread, BringWindowToTop (), SetForegroundWindow(), AttachTheadInput and SetFocus for this. Really way too complicated for a simple task!
I can log in to a site from Firefox using SendInput with keys only. Haven't tried mouse input or Ctrl and Alt keys yet. Also haven't tried other browsers yet.
# 3 Re: How to change input focus to a browser?
WaitForInputIdle accepts exactly two arguments, I have doubt you are using MFC or maybe your code is wrong.
This function must work without need of sleep
WaitForInputIdle ( pi.hProcess, INFINITE );
Also try with WaitForSingleObject, providing second argument INFINITE. I hope this will help you solve this issue.
regards
# 4 Re: How to change input focus to a browser?
Thanks
and Oops, I actually called my own function waitforinputidle ( piid ) that contained WaitForInputIdle ( pi.hProcess, INFINITE ) plus all the error checking that goes with it.
So what I found is that WaitForInputIdle returns before the process window is visible. Waiting by looping on FindWindow handles the synchronization though.
BTW: how do I bracket code? I tried <code> and </code> but that didn't work
# 5 Re: How to change input focus to a browser?
Please do provide little report when you get it working, so that your result may help other readers and dont waste time posting same thing again.
Secondly, you can use code tags use samething <code> but without < and > use square brackets [ and ], or may be goto full box (not quick reply) put some text select text and click http://www.dev-archive.com/forum/images/editor/code.gif button.
regards