• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

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
0
Richard2000
Asked:
Richard2000
  • 2
1 Solution
 
intheCommented:
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
 
intheCommented:
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
 
Richard2000Author Commented:
Hi,

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

Richard
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now