A way to redirect keypresses from TWebBrowser to the ParentForm

 

Questions


First question. Help format it, if needed, please.

Context
I have a TWebBrowser in the main form that is used to behave like it was a printer.
So I load some HTML text in it as user do some commands in the real printer…
I want the user to be able to click and select text from the WebBrowser.

Problem
When the user clicks in the WebBrowser some of the shortcuts registered from the actions don’t work anymore. For example, there is an action with shortcut F7. If the user clicks in the WebBrowser and presses F7, it does not invoke my shortcut.
I know that this is by design of the WebBrowser.

So, I thought: I want to send every key combination back to the form.
The question is: How?
If it was another control, I could use a perform(WM_KeyDown,...) in the OnKeyDown event.

Alternatives or suggestions would be appreciated too. I am very tired these past 2 days, so I could be missing something.

 

 

————————————————-

Answer

Derivate TWebBrowser with an implementation of IDocHostUIHandler or use the famous EmbeddedWB

Implement the interface with an event OnTranslateAccelerator called in TranslateAccelerator

Set the event on your brwoser instance

Detect your key(s) like this:

function TBrowserPageIE.DoTranslateAccelerator(const lpMsg: PMSG; const pguidCmdGroup: PGUID; const nCmdID: DWORD): HRESULT;
begin
  result := S_FALSE;

  if lpMsg.message = WM_KEYDOWN then begin
    if lpMsg.wParam = VK_F7 then begin
      // do something here...
      result := S_OK;
    end;
  end;
end;

delphi,focus,sendkeys,twebbrowser,webbrowser-control

Facebook Comments

Post a comment