The WebViewKeyEventHandled function is used in conjunction with the
WebViewEventProc callback when the application receives a
WEBVIEW_EVENT_ACCELERATOR_KEYPRESS event. By calling this function
and specifying that the key event has been handled, the application can prevent
the browser from processing the key itself.
This mechanism is useful when you want to disable or override the
default behavior of certain keys within the embedded browser. For
example, you may wish to prevent the user from pressing F12 to
open the developer tools, block function keys that have meaning in your
application, or intercept combinations such as Ctrl+P or
Ctrl+S to substitute your own print or save functionality.
If bHandled is non-zero, the event is considered handled by the
host application and WebView2 will not process the key. If bHandled
is zero, the key is passed through and processed normally according to
the browser's default behavior. This function should typically be called
during the event handler for WEBVIEW_EVENT_ACCELERATOR_KEYPRESS.
Note that this function only affects accelerator key events when the
WEBVIEW_OPTION_ENABLE_ACCELERATORS option has been specified when
creating or attaching the WebView. It does not apply to ordinary text
input within form fields or content editable regions in the page.
VOID CALLBACK EventHandler(HWND hWndHost, UINT uEvent, WPARAM wParam, LPARAM lParam, DWORD_PTR dwParam)
{
if (uEvent == WEBVIEW_EVENT_ACCELERATOR_KEYPRESS)
{
UINT vkCode = (UINT)lParam;
if (vkCode == VK_F3) // Intercept the F3 key
{
WebViewKeyEventHandled(hWndHost, TRUE);
return;
}
}
}