Solved

Getting TWebBrowser to accept enter presses (#13)

Posted on 2003-12-06
7
1,178 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 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