Link to home
Start Free TrialLog in
Avatar of Richard2000
Richard2000

asked on

Tab and Delete keys don't work with TWebBrowser

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
Avatar of inthe
inthe

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.
ASKER CERTIFIED SOLUTION
Avatar of inthe
inthe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Richard2000

ASKER

Hi,

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

Richard