TWebBrowser focus problem

Hello!

I have problem with TWebBrowser ActiveX control. Every time WebBrowser gains focus, form stops receiving OnKeyDown/OnKeyPress/OnKeyUp events. This can be workarounded by Application.OnMessage but there's another problem. I have two controls on form, TWebBrowser and TEdit. When I click on WebBrowser, there is no way Edit can regain focus by code (i can still click on Edit ;). Edit1.SetFocus, Form1.ActiveControl and SetFocusedControl don't work.

This is a handler in ApplicationEvents.OnMessage. It doesn't work, focus remains on WebBrowser (and yes I tried - "Test" message would show)

  if (GetKeyState(VK_CONTROL) < 0) and (Msg.Message = WM_KEYDOWN) and
    (Msg.wParam = VK_HOME) then
  begin
   Edit1.SetFocus;
//  Showmessage('Test');
   Handled:=true;
  end;

Since WebBrowser is TWinControl, I suspected it may behave like TForm, but BringToFront and SetForegroundWindow API call didn't work.

Only workaround I have found by now is to hide and show a form (it is unacceptable). Is it a problem with ActiveX or something else?

I use Delphi 2006, Windows Vista x64 and IE7
LVL 1
TiborKiAsked:
Who is Participating?
 
TheRealLokiConnect With a Mentor Senior DeveloperCommented:
I find that if you focus another control first, then it does work

eg.
  button1.SetFocus;
  edit1.SetFocus;

I think the thing is to make your app really think that the edit control is a "new" focus
0
 
TiborKiAuthor Commented:
Thanks, it works.
Only one subquestion: is it possible to somehow simulate keypress so tex would appear in Edit when someone types in TWebBrowser after edit regains focus in Application.OnMessage handler.
0
 
TheRealLokiSenior DeveloperCommented:
you can set the Edit1.SelText property (or even the Edit.Text if you want the whole thing)
or you can do a postmessage(edit1.handle, wm_char, ord('A'), 0); for example
might be sendmessage, not sure
0
 
smotCommented:
or:
    ActiveControl := nil;
    Edit1.SetFocus;
0
 
ejla51Commented:
>ActiveControl := nil;
> Edit1.SetFocus;

Very good solution! I had bloody troubles /w this issue!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.