Solved

Tab and Delete keys don't work with TWebBrowser

Posted on 2002-04-28
3
177 Views
Last Modified: 2010-04-04
Hi,

I would like to make the Tab and Delete keys work with TWebBrowser.  When these keys are pressed and the TWebBrowser has the focus, nothing happens.  I've read in the newsgroups that this is a problem and they say read the FAQs at http://members.home.net/hfournier/.  Unfortunately, this site doesn't seem to be working, so I'm stuck.

Would anyone be able to post any Delphi source code to make these keys work correctly within TWebBrowser?  Also, do you know if any other keys don't work correctly with TWebBrowser, and if so which?

Thanks in Advance,

Richard
0
Comment
Question by:Richard2000
  • 2
3 Comments
 
LVL 17

Expert Comment

by:inthe
ID: 6975426
hi,
yes it is a problem ,there are many oher probs with TWebbrowser and it is really better to use the TEmBeddedWb from :
http://www.euromind.com/iedelphi/
it has way better event handling capabilities ..
even cut'n'pate play up with twebbrowser

as for the twebbrowser you can still use t if you wish but it will need an event handler similar to below:
(also similar to what you would find at henry fourniers site).

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, OleCtrls, SHDocVw_TLB,ActiveX;

type
  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
   OIPAO  : IOleInPlaceActiveObject;
   OIPAO_Wnd:HWND;

  public
    { Public declarations }
    procedure MyMessageHandler(var Msg: TMsg; var Handled: Boolean);
  end;

var
  Form1: TForm1;
  SaveMessageHandler: TMessageEvent;
implementation

{$R *.DFM}

procedure Tform1.MyMessageHandler(var Msg: TMsg; var Handled: Boolean);
const
  DuplicatedKeys: set of Byte = [VK_TAB, VK_BACK, VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN, VK_PRIOR, VK_NEXT];
var
   w:HWND;
begin
     Handled:=False;
     if Msg.message = WM_KEYDOWN then
     begin
          if Msg.wParam in DuplicatedKeys then
         //do nothing let ie take care of it
           end;
     w:=GetFocus;
     if (w=0) {or (w=[].Handle)} then Exit;
     if OIPAO<>nil then
     begin
          if w = OIPAO_Wnd then Handled := IsDialogMessage(w,Msg);
          if not Handled then Handled := (OIPAO.TranslateAccelerator(Msg) = S_OK);
     end;
     if (not Handled) and Assigned(SaveMessageHandler) then SaveMessageHandler(Msg,Handled);
 end;

procedure TForm1.FormCreate(Sender: TObject);
var
   Dispatch:IDispatch;
begin
    if assigned(WebBrowser1) then
     begin
          Dispatch := WebBrowser1.Application_;
          if Dispatch<>nil then
          begin
               Dispatch.QueryInterface(IOleInPlaceActiveObject, OIPAO);
          end;
          if OIPAO <> nil then OIPAO.GetWindow(OIPAO_Wnd);
     end;
     SaveMessagehandler := Application.OnMessage;
     Application.OnMessage:=MyMessageHandler;
 end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
OIPAO := nil;
Application.OnMessage := SaveMessageHandler;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
opendialog1.execute;
webbrowser1.OleObject.navigate(opendialog1.filename);
end;

initialization
  OleInitialize(nil);

finalization
  OleUninitialize;

end.
0
 
LVL 17

Accepted Solution

by:
inthe earned 50 total points
ID: 6975434
ops,
i just got this form my paq:
i think it is the actual one from henrys site but i notice its missing the tab so it needs to be added as i did above.


  procedure MyMessageHandler(var Msg: TMsg; var Handled: Boolean);

    { Public declarations }
  end;

var
  Form1: TForm1;
  FOleInPlaceActiveObject: IOleInPlaceActiveObject;
  SaveMessageHandler: TMessageEvent;


implementation

{$R *.DFM}

procedure TForm1.MyMessageHandler(var Msg: TMsg; var Handled: Boolean);
var
  iOIPAO: IOleInPlaceActiveObject;
  Dispatch: IDispatch;
begin
  { exit if we don't get back a webbrowser object }
  if WebBrowser1 = nil then
  begin
    Handled := False;
    Exit;
  end;

  Handled:=(IsDialogMessage(WebBrowser1.Handle, Msg) = True);

  if (Handled) and (not WebBrowser1.Busy) then
  begin
    if FOleInPlaceActiveObject = nil then
    begin
      Dispatch := WebBrowser1.Application_;
      if Dispatch <> nil then
      begin
        Dispatch.QueryInterface(IOleInPlaceActiveObject, iOIPAO);
        if iOIPAO <> nil then
          FOleInPlaceActiveObject := iOIPAO;
      end;
    end;

    if FOleInPlaceActiveObject <> nil then
      if ((Msg.message = WM_KEYDOWN) or (Msg.message = WM_KEYUP)) and
         ((Msg.wParam = VK_BACK) or (Msg.wParam = VK_LEFT) or (Msg.wParam = VK_RIGHT)) then
        //nothing - do not pass on Backspace, Left or Right arrows
      else
        FOleInPlaceActiveObject.TranslateAccelerator(Msg);
  end;
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
  SaveMessageHandler := Application.OnMessage;
  Application.OnMessage := MyMessageHandler;

end;

procedure TForm1.FormDeactivate(Sender: TObject);
begin
Application.OnMessage := SaveMessageHandler;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Application.OnMessage := SaveMessageHandler;
  FOleInPlaceActiveObject := nil;
end;

procedure TForm1.Button1Click(Sender: TObject);
var a,b,c,d:OleVariant;
begin
     WebBrowser1.Navigate(Edit1.Text,a,b,c,d);
end;

initialization
  OleInitialize(nil);

finalization
  OleUninitialize;
0
 

Author Comment

by:Richard2000
ID: 6982919
Hi,

Many thanks for the code.  I'll take a look at EmbeddedWB too.

Richard
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

13 Experts available now in Live!

Get 1:1 Help Now