TWebBrowser focus problem

Posted on 2007-10-02
Last Modified: 2013-11-18

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
//  Showmessage('Test');

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
Question by:TiborKi
    LVL 17

    Accepted Solution

    I find that if you focus another control first, then it does work


    I think the thing is to make your app really think that the edit control is a "new" focus
    LVL 1

    Author Comment

    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.
    LVL 17

    Expert Comment

    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
    LVL 3

    Expert Comment

        ActiveControl := nil;

    Expert Comment

    >ActiveControl := nil;
    > Edit1.SetFocus;

    Very good solution! I had bloody troubles /w this issue!!!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
    In this article you will learn how to create a free basic website on Bitbucket, a git service provider. Polymer creates dynamic HTML components, which allow more flexibility than static HTML. This tutorial uses Ubuntu Linux but can also be done on W…
    The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now