WebViewKeyEventHandled Function  
 
BOOL WINAPI WebViewKeyEventHandled(
  HWND hWndHost,  
  BOOL bHandled  
);

The WebViewKeyEventHandled function sets an accelerator key event as handled.

Parameters

hWndHost
Handle to the window attached to the browser session.
bHandled
An integer which specifies if the accelerator key event was handled by the application. A non-zero value indicates the application has handled the accelerator key event and the WebView2 control should not process the event. A value of zero specifies that the browser should handle the accelerator key using it's normal, default behavior.

Return Value

If the function succeeds, the return value is non-zero. If the function fails, the return value is zero. To get extended error information, call WebViewGetLastError.

Remarks

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.

Example

VOID CALLBACK EventHandler(HWND hWndHost, UINT uEvent, WPARAM wParam, LPARAM lParam, DWORD_PTR dwParam)
{
    if (uEvent == WEBVIEW_EVENT_ACCELERATOR_KEYPRESS)
    {
        // wParam contains flags (WEBVIEW_ACCELERATOR_KEY_DOWN/UP/SYSTEM)
        // lParam contains the virtual-key code
        UINT vkCode = (UINT)lParam;

        if (vkCode == VK_F3) // Intercept the F3 key
        {
            // Mark the event as handled so WebView2 won't process it
            WebViewKeyEventHandled(hWndHost, TRUE);
            return;
        }
    }

    // Other events can be handled here as needed
}

Requirements

Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: cshtmv12.lib

See Also