Solved

Getting TWebBrowser to accept enter presses (#13)

Posted on 2003-12-06
7
1,143 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
Comment Utility
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:
mnasman earned 250 total points
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 22

Expert Comment

by:mnasman
Comment Utility
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
Comment Utility
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
Comment Utility
This is the way to do it:
wb1 : webbrowser


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

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

10 Experts available now in Live!

Get 1:1 Help Now