?
Solved

Getting TWebBrowser to accept enter presses (#13)

Posted on 2003-12-06
7
Medium Priority
?
1,203 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

719 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