Skip to content

cWebView2Host Events Reference

📋 Events Overview

cWebView2Host provides 40+ events covering the entire WebView2 lifecycle. To use events, declare the variable with WithEvents:

vb
Dim WithEvents wv As cWebView2Host

Event Category Index

CategoryEventTrigger Timing
LifecycleCreateWebView2 control creation completed
ReadyFirst navigation completed, control ready
ErrorError during creation or runtime
NavigationNavigationStartingNavigation starting (cancelable)
NavigationCompleteNavigation completed
SourceChangedURL changed
DocumentTitleChangedDocument title changed
DOMContentLoadedDOM loading completed
Permission/DialogPermissionRequestedPage requests permission
ScriptDialogOpeningScript dialog appearing (alert/confirm/prompt)
AcceleratorKeyPressedAccelerator key pressed
NewWindowRequestedPage requests opening new window
ResourcesWebResourceRequestedWeb resource request intercepted
ProcessFailedBrowser process failed
DownloadStartingDownload about to start
ScriptingJsAsyncResultAsync script execution completed
JsMessageJS postMessage received
DevToolsProtocolResponseCDP async response
Host MouseHostMouseDownMouse down in host area
HostMouseUpMouse up in host area
HostMouseDblClickMouse double-click in host area
HostMouseMoveMouse move in host area
HostMouseWheelMouse wheel in host area
HostContextMenuContext menu in host area
Host KeyboardHostKeyDownKey down in host area
HostKeyUpKey up in host area
HostKeyPressKey press in host area
HostFocusWebView2 gained focus
HostBlurWebView2 lost focus
HostResizeHost window size changed
User MouseUserMouseDownMouse down in content area
UserMouseUpMouse up in content area
UserMouseMoveMouse move in content area
UserMouseWheelMouse wheel in content area
UserContextMenuContext menu in content area
UserDblClickMouse double-click in content area
SuspendSuspendCompletedSuspend succeeded
SuspendFailedSuspend failed
PDFPrintToPdfCompletedPDF print succeeded
PrintToPdfFailedPDF print failed
BusinessOnGetCookiesFullAsync cookie retrieval completed

🔄 Lifecycle Events

Create

WebView2 control creation completed, but navigation has not yet started.

vb
Private Sub wv_Create()

Trigger Timing: After WebView2 Environment and Controller are successfully created.

Purpose: This is the correct timing to configure EnvironmentOptions and Security, since the environment is created but navigation has not yet started.

Example:

vb
Private Sub wv_Create()
    ' Configure user data directory
    wv.EnvironmentOptions.UserDataFolder = App.Path & "\UserDir\account-001"
    ' Configure security options
    wv.Security.CertificateErrorAction = CEA_AlwaysAllow
End Sub

Ready

WebView2 first navigation completed, control fully ready.

vb
Private Sub wv_Ready()

Trigger Timing: Fires once after the first navigation completes. Subsequent navigations do not trigger this event.

Purpose: Safely perform initialization operations such as binding events, registering data bindings, injecting scripts, etc.

Example:

vb
Private Sub wv_Ready()
    Me.Caption = wv.DocumentTitle
    
    ' Set up data bindings
    wv.BindData "name", "#name-input", "value"
    wv.BindUI Me, "OnNameInput", "#name-input", EventName:="input"
    wv.SetData "name", "Default value"
    
    ' Inject script
    wv.AddScriptToExecuteOnDocumentCreated "window.__appReady = true;"
End Sub

Error

Error during creation or runtime.

vb
Private Sub wv_Error(ByVal Description As String)

Parameters:

  • Description - Error description message

🧭 Navigation Events

Navigation is about to start — can be canceled via the Cancel parameter.

vb
Private Sub wv_NavigationStarting(ByVal Uri As String, ByVal IsUserInitiated As Boolean, ByRef Cancel As Boolean)

Parameters:

  • Uri - Target URL
  • IsUserInitiated - Whether triggered by user action (e.g., clicking a link)
  • Cancel - Set to True to cancel navigation

Example:

vb
Private Sub wv_NavigationStarting(ByVal Uri As String, ByVal IsUserInitiated As Boolean, ByRef Cancel As Boolean)
    ' Block navigation to specific domain
    If InStr(UCase(Uri), "ADS.EXAMPLE.COM") > 0 Then
        Cancel = True
    End If
End Sub

Navigation completed.

vb
Private Sub wv_NavigationComplete(ByVal IsSuccess As Boolean, ByVal ErrorStatus As wv2ErrorStatus)

Parameters:

  • IsSuccess - Whether navigation succeeded
  • ErrorStatus - Error status code (0 on success)

SourceChanged

URL changed.

vb
Private Sub wv_SourceChanged(ByVal IsNewDocument As Boolean)

Parameters:

  • IsNewDocument - Whether this is a new document (vs. same-page navigation)

DocumentTitleChanged

Document title changed.

vb
Private Sub wv_DocumentTitleChanged()

Example:

vb
Private Sub wv_DocumentTitleChanged()
    Me.Caption = wv.DocumentTitle
End Sub

DOMContentLoaded

DOM content loading completed (earlier than NavigationComplete).

vb
Private Sub wv_DOMContentLoaded()

Purpose: Perform operations when DOM is ready but resources (images, etc.) have not fully loaded.


🔐 Permission/Dialog Events

PermissionRequested

Page requests permission (geolocation, camera, microphone, etc.).

vb
Private Sub wv_PermissionRequested(ByVal Uri As String, ByVal PermissionKind As wv2PermissionKind, ByVal IsUserInitiated As Boolean, ByRef State As wv2PermissionState)

Parameters:

  • Uri - URL of the page requesting permission
  • PermissionKind - Permission type enum
  • IsUserInitiated - Whether triggered by user action
  • State - Permission state, can be set to wv2PermissionState_Allow or wv2PermissionState_Deny

Example:

vb
Private Sub wv_PermissionRequested(ByVal Uri As String, ByVal PermissionKind As wv2PermissionKind, ByVal IsUserInitiated As Boolean, ByRef State As wv2PermissionState)
    ' Auto-allow geolocation from trusted domains
    If PermissionKind = wv2PermissionKind_Geolocation And InStr(Uri, "myapp.com") > 0 Then
        State = wv2PermissionState_Allow
    Else
        State = wv2PermissionState_Deny
    End If
End Sub

ScriptDialogOpening

JavaScript dialog about to appear (alert / confirm / prompt / beforeunload).

vb
Private Sub wv_ScriptDialogOpening(ByVal Kind As wv2ScriptDialogKind, ByVal Message As String, ByRef Accept As Boolean)

Parameters:

  • Kind - Dialog type
  • Message - Dialog message content
  • Accept - Set to True to accept the dialog (equivalent to clicking OK), False to cancel

Note: Can be used to customize dialog appearance, replacing the browser's default dialogs.


AcceleratorKeyPressed

Accelerator key pressed event.

vb
Private Sub wv_AcceleratorKeyPressed(ByVal KeyEventKind As wv2KeyEventKind, ByVal VirtualKey As Long, ByRef Handled As Boolean)

Parameters:

  • KeyEventKind - Key event type (KeyDown/Up/SystemKeyDown/Up)
  • VirtualKey - Virtual key code
  • Handled - Set to True to prevent WebView2 from handling this key

Example:

vb
Private Sub wv_AcceleratorKeyPressed(ByVal KeyEventKind As wv2KeyEventKind, ByVal VirtualKey As Long, ByRef Handled As Boolean)
    ' Intercept F5 refresh
    If VirtualKey = vbKeyF5 Then
        Handled = True
        ' Custom refresh logic
    End If
End Sub

NewWindowRequested

Page requests opening a new window (target="_blank" or window.open).

vb
Private Sub wv_NewWindowRequested(ByVal Uri As String, ByVal IsUserInitiated As Boolean, ByRef Handled As Boolean)

Parameters:

  • Uri - Target URL of the new window
  • IsUserInitiated - Whether triggered by user action
  • Handled - Set to True to prevent opening a new window

Example:

vb
Private Sub wv_NewWindowRequested(ByVal Uri As String, ByVal IsUserInitiated As Boolean, ByRef Handled As Boolean)
    ' Navigate in current window (block popup)
    Handled = True
    wv.Navigate Uri
End Sub

📦 Resource Events

WebResourceRequested

Web resource request intercepted by AddWebResourceRequestedFilter.

vb
Private Sub wv_WebResourceRequested(ByVal Request As WebView2Request, ByVal Response As WebView2Response)

Parameters:

  • Request - Request object, can read Method, Uri, Headers, Content
  • Response - Response object; after setting values, WebView2 uses this response instead of the actual network request

Note: Must call AddWebResourceRequestedFilter to register a filter before this event will fire.


ProcessFailed

Browser process failed.

vb
Private Sub wv_ProcessFailed(ByVal FailedKind As wv2ProcessFailedKind)

Parameters:

  • FailedKind - Process failure type enum

DownloadStarting

Download about to start.

vb
Private Sub wv_DownloadStarting(ByVal ResultFilePath As String, ByRef Cancel As Boolean, ByRef Handled As Boolean)

Parameters:

  • ResultFilePath - Download file path
  • Cancel - Set to True to cancel download
  • Handled - Set to True to indicate handled

⚡ Script Events

JsAsyncResult

Async script execution completed (callback for ExecuteScript / JsRunAsync).

vb
Private Sub wv_JsAsyncResult(ByVal Token As Long, ByVal Result As String, ByVal ErrorCode As Long)

Parameters:

  • Token - Token specified at call time
  • Result - JSON string of JS execution result
  • ErrorCode - Error code, 0 indicates success

Example:

vb
wv.ExecuteScript "document.title", 1001
wv.JsRunAsync "fetch('/api').then(r=>r.json())"  ' Returns auto token

Private Sub wv_JsAsyncResult(ByVal Token As Long, ByVal Result As String, ByVal ErrorCode As Long)
    If ErrorCode = 0 Then
        Debug.Print "Token " & Token & " => " & Result
    End If
End Sub

JsMessage

Message received from JS via window.chrome.webview.postMessage().

vb
Private Sub wv_JsMessage(ByVal Message As String)

Parameters:

  • Message - Message string sent from JS

Example:

vb
' VB6 side
Private Sub wv_JsMessage(ByVal Message As String)
    Debug.Print "JS message: " & Message
End Sub

' JS side
window.chrome.webview.postMessage("Hello from JS");

DevToolsProtocolResponse

CDP async call response.

vb
Private Sub wv_DevToolsProtocolResponse(ByVal CustomEventId As Variant, ByVal JsonResponse As String)

Parameters:

  • CustomEventId - Custom event ID specified at call time
  • JsonResponse - JSON string of CDP response

Example:

vb
wv.CallDevToolsProtocolMethod "Page.captureScreenshot", "{}", "screenshot"

Private Sub wv_DevToolsProtocolResponse(ByVal CustomEventId As Variant, ByVal JsonResponse As String)
    If CustomEventId = "screenshot" Then
        ' Process screenshot data
    End If
End Sub

🖱️ Host Mouse Events

Host mouse events are captured by subclassing the host window and reflect mouse operations in the host window area (not the WebView2 content area).

HostMouseDown

vb
Private Sub wv_HostMouseDown(ByVal Button As wv2MouseButton, ByVal Shift As wv2ShiftState, ByVal X As Long, ByVal Y As Long)

Parameters:

  • Button - Mouse button (1=Left, 2=Right, 4=Middle, bitmask)
  • Shift - Modifier key state (1=Shift, 2=Ctrl, 4=Alt, bitmask)
  • X, Y - Mouse coordinates (client area pixels)

HostMouseUp

vb
Private Sub wv_HostMouseUp(ByVal Button As wv2MouseButton, ByVal Shift As wv2ShiftState, ByVal X As Long, ByVal Y As Long)

HostMouseDblClick

vb
Private Sub wv_HostMouseDblClick(ByVal Button As wv2MouseButton, ByVal Shift As wv2ShiftState, ByVal X As Long, ByVal Y As Long)

HostMouseMove

vb
Private Sub wv_HostMouseMove(ByVal Button As wv2MouseButton, ByVal Shift As wv2ShiftState, ByVal X As Long, ByVal Y As Long)

Note: Requires EnableMouseMoveEvents = True to fire; disabled by default to avoid performance issues.


HostMouseWheel

vb
Private Sub wv_HostMouseWheel(ByVal Delta As Long, ByVal Shift As wv2ShiftState, ByVal X As Long, ByVal Y As Long)

Parameters:

  • Delta - Wheel delta, positive for scroll up, negative for scroll down

HostContextMenu

vb
Private Sub wv_HostContextMenu(ByVal X As Long, ByVal Y As Long)

⌨️ Host Keyboard Events

HostKeyDown / HostKeyUp / HostKeyPress

vb
Private Sub wv_HostKeyDown(ByVal KeyCode As Long, ByVal Shift As wv2ShiftState)
Private Sub wv_HostKeyUp(ByVal KeyCode As Long, ByVal Shift As wv2ShiftState)
Private Sub wv_HostKeyPress(ByVal KeyAscii As Long)

HostFocus / HostBlur

vb
Private Sub wv_HostFocus()
Private Sub wv_HostBlur()

HostResize

vb
Private Sub wv_HostResize()

Note: Fires when host window size changes; can call wv.Resize() here to manually adjust WebView2 size (e.g., when using MessageWindowAdapter).


🎯 User Mouse Events

User mouse events are synchronously called back from the WebView2 content area to the host via the mouseProxy COM proxy. Requires EnableUserMouseEvents = True.

UserMouseDown / UserMouseUp

vb
Private Sub wv_UserMouseDown(ByVal Button As Long, ByVal X As Long, ByVal Y As Long, ByVal Shift As Long, ByRef Handled As Boolean)
Private Sub wv_UserMouseUp(ByVal Button As Long, ByVal X As Long, ByVal Y As Long, ByVal Shift As Long, ByRef Handled As Boolean)

Parameters:

  • Button - Mouse button (1=Left, 2=Right, 4=Middle)
  • X, Y - Pixel coordinates within content area
  • Shift - Modifier key state
  • Handled - Set to True to prevent event from propagating to WebView2

UserMouseMove

vb
Private Sub wv_UserMouseMove(ByVal X As Long, ByVal Y As Long, ByVal Shift As Long, ByRef Handled As Boolean)

Note: Requires additional EnableUserMouseMove = True; disabled by default to avoid performance issues.


UserMouseWheel

vb
Private Sub wv_UserMouseWheel(ByVal Delta As Long, ByVal X As Long, ByVal Y As Long, ByVal Shift As Long, ByRef Handled As Boolean)

UserContextMenu

vb
Private Sub wv_UserContextMenu(ByVal X As Long, ByVal Y As Long, ByRef Handled As Boolean)

UserDblClick

vb
Private Sub wv_UserDblClick(ByVal Button As Long, ByVal X As Long, ByVal Y As Long, ByVal Shift As Long, ByRef Handled As Boolean)

🔌 Suspend/PDF/Business Events

SuspendCompleted / SuspendFailed

vb
Private Sub wv_SuspendCompleted()
Private Sub wv_SuspendFailed(ByVal ErrorCode As Long)

PrintToPdfCompleted / PrintToPdfFailed

vb
Private Sub wv_PrintToPdfCompleted(ByVal ResultFilePath As String)
Private Sub wv_PrintToPdfFailed(ByVal ErrorCode As Long)

OnGetCookiesFull

Async full cookie retrieval completed.

vb
Private Sub wv_OnGetCookiesFull(ByVal CookiesJson As String)

Trigger Timing: When async result returns after calling wv.Cookies.GetCookiesFullAsync().


📐 Enum Reference

wv2MouseButton

ConstantValueDescription
Left1Left mouse button
Right2Right mouse button
Middle4Middle mouse button

Bitmask combination, can include multiple values simultaneously.

wv2ShiftState

ConstantValueDescription
Shift1Shift key
Ctrl2Ctrl key
Alt4Alt key

Bitmask combination. Detection method: If Shift And 2 Then ' Ctrl is pressed

wv2ScriptDialogKind

ConstantDescription
Alertalert() dialog
Confirmconfirm() dialog
Promptprompt() dialog
BeforeUnloadbeforeunload dialog

wv2PermissionKind

ConstantDescription
UnknownUnknown permission
MicrophoneMicrophone
CameraCamera
GeolocationGeolocation
NotificationsNotifications
OtherSensorsOther sensors
ClipboardReadClipboard read

wv2PermissionState

ConstantDescription
DefaultDefault (browser decides)
AllowAllow
DenyDeny

EnumCertificateErrorAction

ConstantValueDescription
CEA_Default0Default behavior (deny)
CEA_Cancel1Cancel request
CEA_AlwaysAllow2Always allow (injects command-line argument)

Last Updated: 2026-06-24

VB6 and LOGO copyright of Microsoft Corporation