How to get mouse events from WebView2 to host program in C++

  c++, mfc, mouseevent, webview2, windows

I am trying to get mouse events from WebView2 to host program in c++. Not sure if we have to inject JS code to do this, but I want mouse events such as onclick -> to get element ID of the button clicked.
I am using WebView2GettingStarted sample application from MSDN and trying something like –

// Schedule an async task to add initialization script that  
// 1) Add an listener to print message from the host  
// 2) Post document URL to the host
    
    webviewWindow->AddScriptToExecuteOnDocumentCreated( L"window.chrome.webview.addEventListener('message', event => alert(event.data));" L"window.chrome.webview.postMessage(alert('NewCode'));",nullptr);

Source: Windows Questions

LEAVE A COMMENT