How to capture OnKeyDown in TEdgeBrowser

187 views Asked by At

I am aware of the technique of adding injecting onkeydown JavaScript event into the HTML and using web message to return the code back:

EdgeBrowser.ExecuteScript("window.onkeydown = function(ev) { window.chrome.webview.postMessage(ev.keyCode); };");

And later in TEdgeBrowser OnWebMessageReceived event fetching the code from Args parameter.

procedure TfrmMain.EdgeBrowserWebMessageReceived(Sender: TCustomEdgeBrowser; Args: TWebMessageReceivedEventArgs);
begin
  // parse received WebMessage
end;

The problem is though SettingsInterface.Set_IsScriptEnabled is set to false, JavaScript events stop working as well.

Is there a way to capture keyboard events in TEdgeBrowser without the use of JavaScript and the above technique?

I also tried:

  • OnKeyDown event of the TForm, with KeyPreview set to true. No effect
  • ApplicationEvents - OnMessage WM_KEYDOWN and WM_KEYUP - no effect either.
1

There are 1 answers

1
Salvador Díaz Fau On

Consider using WebView4Delphi instead and try the WindowlessBrowser demo. It uses the Windowless mode in WebView2 and you can intercept WM_KEY* messages with a simple TApplicationEvents.

That mode uses the DirectComposition API which is not supported by Delphi but you can use the MfPack component.

That demo is almost a direct translation of the code in the official WebView2APISample available at the WebView2Samples repository.