Getting keys to work in WebBrowser

Hi there,

I'm making a little program for playing flash games without a internet connection, it's ment for my coworkers when they want to play a little game when they have nothing to do, without having to go on internet.

The games are flashgames (like pacman andso), which i download from internet.

I use the WebBrowser component to navigate to the <filename>.swf file, and that works okay.

But then, after you click <PLAY>, the game starts, pacman (in this example) starts eating his way, but you can't send him in any direction...bummer...

I tried to made the WebBrowser component the active one the form, but no luck.
Even when I first click in the browser component, no luck.

In IE6 it's works fine with just the <filename>.swf file, but as soon as mine program starts, it doesn't work in my program.

It's very simpel for now, just a form, a menu bar with a start option and a webbrowser component. But before i go making the program be able to play more games, update itself etc., i first want to get this problem fixed.

For the moment i'm talking about the cursor keys, but ofcourse al keys have to go directly to the webbrowser component.

What am i doing wrong ?
WMPetersAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DaFoxCommented:
Hi.

Add this to your unit:

initialization
  CoInitialize(nil);

finalization
  CoUninitialize;

Markus
0
WMPetersAuthor Commented:
Markus, i get the following error :

[Error] Unit1.pas(46): Undeclared identifier: 'CoInitialize'
[Error] Unit1.pas(49): Undeclared identifier: 'CoUnitialize'

Some unit i forgot ?
0
DaFoxCommented:
> Some unit i forgot ?

Yep, my fault, sorry.
You have to include ActiveX -> uses ActiveX ;-)

Markus
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

WMPetersAuthor Commented:
Yep, i found that unit, but still doesn't work.

The code now looks like this: (still dirty)
Any ideas?

---

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Close1: TMenuItem;
    StatusBar1: TStatusBar;
    wb1: TWebBrowser;
    Pacman1: TMenuItem;
    procedure Close1Click(Sender: TObject);
    procedure Pacman1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


procedure TForm1.Close1Click(Sender: TObject);
begin
application.Terminate;
end;

procedure TForm1.Pacman1Click(Sender: TObject);
begin
  wb1.navigate('file:///c:/PacMan.Swf');
end;

Initialization
 CoInitialize(nil);


Finalization
 CoUninitialize;

end.
0
DaFoxCommented:
Okaydokay...

Now we got 2 possibilities:

1. We set up our own message handler:
...
  private
    FOleInPlaceActiveObject: IOleInPlaceActiveObject;
    procedure MyMessageHandler(var Msg: TMsg; var Handled: Boolean);

...

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
    begin
      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;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.OnMessage := MyMessageHandler;
end;

2. -> http://community.borland.com/article/0,1410,6302,00.html (if we don't succeed using possibility 1 ;-)

Markus
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WMPetersAuthor Commented:
Yep, i found that unit, but still doesn't work.

The code now looks like this: (still dirty)
Any ideas?

---

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Close1: TMenuItem;
    StatusBar1: TStatusBar;
    wb1: TWebBrowser;
    Pacman1: TMenuItem;
    procedure Close1Click(Sender: TObject);
    procedure Pacman1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


procedure TForm1.Close1Click(Sender: TObject);
begin
application.Terminate;
end;

procedure TForm1.Pacman1Click(Sender: TObject);
begin
  wb1.navigate('file:///c:/PacMan.Swf');
end;

Initialization
 CoInitialize(nil);


Finalization
 CoUninitialize;

end.
0
WMPetersAuthor Commented:
Works fine, now hopefully i can get other keys to work for other games.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.