Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1216
  • Last Modified:

Getting TWebBrowser to accept enter presses (#13)

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
E-T
Asked:
E-T
  • 3
  • 2
  • 2
1 Solution
 
E-TAuthor Commented:
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
 
Mohammed NasmanSoftware DeveloperCommented:
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
 
E-TAuthor Commented:
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
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!

 
Mohammed NasmanSoftware DeveloperCommented:
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
 
E-TAuthor Commented:
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
 
hanson04Commented:
This is the way to do it:
wb1 : webbrowser


  if key = vk_return then
    wb1.Navigate(edit1.Text);
0
 
hanson04Commented:
o i forgot put this in the edit1.onkeydown and not onkeypress
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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