?
Solved

Capture keys from TWebBrowser

Posted on 2009-04-23
5
Medium Priority
?
925 Views
Last Modified: 2013-11-23
Delphi 6 in Windows Xp.
I have a TWebBrowser control in a form, and I would like to get the keys pressed when the focus in on the TWebBrowser (for example: control keys, or ESC key, or other keys).

I tried to use KeyPreview in association with OnKey*** events, unsuccesfully.
I tried to put an event handler in my form (as I read in many places, for example http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_10225776.html), unsuccessfully (see code in code section).

Can you help me, please?
Many thanks in advance.
Federico.
This is the message handler code I added to my form:
 
Tdlg_DOC_edit = class(TForm)
 ...
   wb: TWebBrowser;
   FOleInPlaceActiveObject: IOleInPlaceActiveObject;
   SaveMessageHandler: TMessageEvent;
   procedure message_handler(var Msg: TMsg; var Handled: Boolean);
end;
 
procedure Tdlg_DOC_edit.FormCreate(Sender: TObject);
begin
   SaveMessagehandler := Application.OnMessage;
   Application.OnMessage := message_handler
end;
 
procedure Tdlg_DOC_edit.message_handler(var Msg : TMsg;var Handled : Boolean);
const STANDARD_KEYS = [VK_BACK, VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT];
var
 iOIPAO : IOleInPlaceActiveObject;
 Dispatch : IDispatch;
begin
// exit if we don't get back a webbrowser object
 if (wb = NIL) then begin Handled := False;Exit end;  
 
 Handled := IsDialogMessage(wb.Handle, Msg);
 if Handled AND NOT wb.Busy then begin
  if (FOleInPlaceActiveObject = NIL) then begin
   Dispatch := wb.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 in STANDARD_KEYS)
   then //nothing - do not pass on Backspace, Left or Right arrows
   else FOleInPlaceActiveObject.TranslateAccelerator(Msg)
 end
end;
 
initialization
   OleInitialize(NIL)
finalization
   OleUnInitialize
end.

Open in new window

0
Comment
Question by:feaci
  • 2
4 Comments
 
LVL 3

Expert Comment

by:smot
ID: 24232637
TEmbeddedWB has OnKeyDown/KeyKeyUp events
0
 

Author Comment

by:feaci
ID: 24239725
Thank you for your very useful suggestion, Smot.
Using your TEmbeddedWB component  I could understand that both my code and your component work only when navigating HTML pages.  When reading a PDF or a DOC file  (or others files) my code and TEmbeddedWB do not fire events for pressed keys.
My question is therefore a little bit different, or applies to a slightly different context: how can I get keys form TWebBrowser (or TEmbeddedWB) when navigating in non-html files?
I tried also using an event handler for the application, but it doesn't work.
0
 
LVL 3

Assisted Solution

by:smot
smot earned 1000 total points
ID: 24247715
try installing a local keyboard hook
0
 
LVL 26

Accepted Solution

by:
Eddie Shipman earned 1000 total points
ID: 25118548
That is correct because the embedded program takes over the handling, you will need to install a system-wide keyboard hook for this.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

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 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…
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Suggested Courses

589 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