mobileFX WebKitX CEF3 ActiveX 4.x
WebKitXCEF3Lib ActiveX Control / WebKitXCEF3 Object / AddEventListener Method

CSS3 selector to bind the event handler to. This can also be window and document.

If you are unfamiliar with CSS3 Selectors have a look in Finding the correct CSS3 Selector article.

The event name.

A Visual Basic public module function passed using the AddressOf operator.

Event capture flag.

This flag sets Asynchronous or Synchronous event handling. In synchronous events you can cancel the event and its bubble.

In This Topic
    AddEventListener Method
    In This Topic
    Description

    Binds a native event handler to an HTML5 event. This is the AddressOf method.

    Syntax
    Visual Basic
    Public Sub AddEventListener( _
       ByVal Selector As String, _
       ByVal Event As String, _
       ByVal AddressOfEventHandler As Long, _
       ByVal UseCapture As Boolean, _
       ByVal Async As Boolean _
    ) 
    Parameters
    Selector

    CSS3 selector to bind the event handler to. This can also be window and document.

    If you are unfamiliar with CSS3 Selectors have a look in Finding the correct CSS3 Selector article.

    Event

    The event name.

    AddressOfEventHandler

    A Visual Basic public module function passed using the AddressOf operator.

    UseCapture

    Event capture flag.

    Async

    This flag sets Asynchronous or Synchronous event handling. In synchronous events you can cancel the event and its bubble.

    Remarks

    Event listeners are automatically cleared when a new page is loaded.

    Asynchronous DOM to COM Events

    HTML5 DOM Events are generated by WebKit Blink Engine and are copied into an immutable format before they are transmitted from CEFXClient process to WebKitX ActiveX. Once WebKitX ActiveX receives an event notification through IPC, it fires a COM Event with the copied data of HTML DOM event. Thus, CEFXClient process does not block waiting for your client code to handle the event. WebKitX DOM Events are Immutable, meaning that you can only read event data but you cannot cancel events, stop them from bubbling or prevent default behavior.

    Synchronous DOM to COM Events

    WebKitX as of version 1.5.11.2591 supports synchronous DOM to COM events by implementing CefMessageRouter circuit. Events generated from WebKit Blink Engine are serialized and passed from Rendering to Browser process, which transmits them to the ActiveX. The browser process waits for the ActiveX to handle the event, where you can also cancel event bubble or prevent event's default behavior.

    Please note that blocking Inter-process Communication (IPC) between 3 processes is not advisable for casual event handling and you should do so only if you need to cancel specific events. Synchronous DOM to COM Events are thread-safe and care has been placed in order to support this feature seamlessly in single-threaded Applications without deadlocks. For that purpose, synchronous events are serialized using mutex synchronization on entry, meaning that synchronous events occupy the IPC channel one at a time. Please contact us for more implementation details or if you need a different handling of DOM to COM events.

     

    Example
    Please have a look in Samples\VB6\Events\Events_Sample.vbp
    Option Explicit
    
    Private Sub WebKitXCEF31_OnBrowserReady()
        ' Load some HTML when browser is created
        WebKitXCEF31.HTML = "<input id='txt1' value='Type Something'/><button id='btn1'>Click ME</button>"
    End Sub
    
    Private Sub WebKitXCEF31_OnLoadEnd()
        
        ' Method 1: Capture events using generic event handler
        WebKitXCEF31.Events = DOM_EVENT_CLICK
        
        ' Always fire WebKitX_OnEvent
        WebKitXCEF31.FireOnEventForAllEvents = False
        
        cmdApply_Click
        
    End Sub
    
    Private Sub cmdApply_Click()
        
        ' Remove previous event handlers (if any)
        WebKitXCEF31.RemoveEventListener "#btn1", "click", AddressOf Module1.MyOnEvent1, True
        WebKitXCEF31.RemoveEventListenerEx "#btn1", "click", Me, "MyOnEvent2", True
        
        ' Method 2: Capture events of button using the AddressOf method
        WebKitXCEF31.AddEventListener "#btn1", "click", AddressOf Module1.MyOnEvent1, True, chkAsyncEvents.Value
        
        ' Method 3: Capture events of button using the IDispatch method
        WebKitXCEF31.AddEventListenerEx "#btn1", "click", Me, "MyOnEvent2", True, chkAsyncEvents.Value
        
    End Sub
    
    Private Sub WebKitXCEF31_OnEvent(ByVal EventType As String, _
                                     ByVal EventSelector As String, _
                                     ByVal TargetSelector As String, _
                                     ByVal TargetPath As String, _
                                     ByVal EventData As String, _
                                     ByVal Async As Boolean, _
                                     PreventDefault As Boolean, _
                                     CancelBubble As Boolean)
    
        AddLog "Received event " + EventType + " from selector " + EventSelector + " for target " + TargetSelector + " using the generic event listener"
    
    End Sub
    
    Public Sub MyOnEvent2(ByVal EventType As String, _
                          ByVal EventSelector As String, _
                          ByVal TargetSelector As String, _
                          ByVal TargetPath As String, _
                          ByVal EventData As String, _
                          ByVal Async As Boolean, _
                          PreventDefault As Boolean, _
                          CancelBubble As Boolean)
                          
        AddLog "Received event " + EventType + " from selector " + EventSelector + " for target " + TargetSelector + " using the IDispatch method"
        ReadTextBox
        
    End Sub
    
    Private Sub AddLog(ByVal Message As String)
        Text2.Text = Text2.Text & Timer & " - " & Message & vbCrLf
    End Sub
    
    Private Sub cmdClearLog_Click()
        Text2.Text = ""
    End Sub
    
    Public Sub ReadTextBox()
        DoEvents
        Text1.Text = WebKitXCEF31.Eval("txt1.value")
    End Sub
    
    
    
    -- Module --
    
    
    Option Explicit
    
    Public Sub MyOnEvent1(ByVal EventType As String, _
                          ByVal EventSelector As String, _
                          ByVal TargetSelector As String, _
                          ByVal TargetPath As String, _
                          ByVal EventData As String, _
                          ByVal Async As Boolean, _
                          PreventDefault As Boolean, _
                          CancelBubble As Boolean)
        
        Form1.Text2.Text = Form1.Text2.Text & Timer & " - Received event " + EventType + " from selector " + EventSelector + " for target " + TargetSelector + " using the AddressOf method" + vbCrLf
        
        PreventDefault = True
        CancelBubble = True
        
        
    End Sub
    See Also