Solved

Getting TWebBrowser to accept enter presses (#13)

Posted on 2003-12-06
7
1,152 Views
Last Modified: 2007-12-19
Hi,

I'm having a rather annoying problem with TWebBrowser.

I have a textarea in a form which I am loading into the TWebBrowser. Theoretically, when I press enter on the textarea, it should insert a line return, it does not. Also, if you press enter on any other control, it should submit the form, it isn't doing that either.

I have tested the same page in Internet Explorer and it works fine. I also added some javascript which responds when key 13 is pressed. When I press enter in Internet Explorer, the event fires twice. In the TWebBrowser control, it only fires once. All other key input is working fine. Does anyone know how to fix this? TWebBrowser does not seem to support any keydown events that I could foward a keypress to. (if i was to intercept it at TForm level)

Any ideas?
0
Comment
Question by:E-T
  • 3
  • 2
  • 2
7 Comments
 

Author Comment

by:E-T
ID: 9890437
Just by the way. I've noticed another thing.

If I hold down alt and type 013, it inserts the return character. It seems to be a problem with explicitly pressing the enter key.
0
 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 250 total points
ID: 9895615
Hello

   I found this code from the net, it will allow the enter key to work with help of IOleInPlaceActiveObject interface, here's the small project that will alow enter to work

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtrls, SHDocVw, StdCtrls, activex;

type
  TForm1 = class(TForm)
    Button1: TButton;
    WebBrowser1: TWebBrowser;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormActivate(Sender: TObject);
    procedure FormDeactivate(Sender: TObject);
  private
    procedure MyMessageHandler(var Msg: TMsg; var Handled: Boolean);
  public
    { Public declarations }
  end;

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

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  webbrowser1.Navigate(edit1.text);
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.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;

Regards,
Mohammed
0
 

Author Comment

by:E-T
ID: 9915245
Thanks a bunch. ;)

The code you gave works very nicely, although I do notice that occationally it still ignores the enter key. It might have something to do with the first few times you press enter. I've been unable to determine what causes it.

Anyhow, it's good enough, the points are yours. May you bask in all their pointy glory. ;)

- ET
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 9918518
LOL, thanx for the points ET
BTW, The code worked fine with me with Enter Key, I post the tested project that I made, so I don't know why it's not worked with you

if you would, give me your email, and I will send you the project
0
 

Author Comment

by:E-T
ID: 9923006
Hi,

I made a test project using the code you posted there. I didn't change anything. It isn't that it's not working, most of the time it works fine. Occationally, I'll need to press enter more than once before it notices. As I said, I don't know what the conditions that cause it are.

Send the the project if you like though - ET@-DeleteThis-Ether.org.za

In case you were wondering, delete -DeleteThis- before sending. ;P

-ET
0
 

Expert Comment

by:hanson04
ID: 9932062
This is the way to do it:
wb1 : webbrowser


  if key = vk_return then
    wb1.Navigate(edit1.Text);
0
 

Expert Comment

by:hanson04
ID: 9932066
o i forgot put this in the edit1.onkeydown and not onkeypress
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

867 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

21 Experts available now in Live!

Get 1:1 Help Now