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:
OnKeyDownevent of the TForm, withKeyPreviewset to true. No effect- ApplicationEvents -
OnMessageWM_KEYDOWNandWM_KEYUP- no effect either.
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.