How to intercept Internet Explorer keyboard shortcuts,
IE events and HTML events: C#, VB.NET

Add-in Express™
for Internet Explorer® and Microsoft® .net

Add-in Express Home > Add-in Express for Internet Explorer > Online Guide > Intercepting IE and HTML events, keyboard shortcuts

IE events, HTML events, keyboard shortcuts

Internet Explorer add-on, IE toolbar and IE bar projects created with Add-in Express for IE and .NET support intercepting three event types: IE events, HTML events and keyboard shortcuts. The sections below provide useful code samples demonstrating what you can do in these events:

Intercepting IE events

IE provides a number of events such as DownloadComplete or BeforeNavigate2. All such events are mapped to the corresponding events of the IEModule class. The following code samples demonstrate a possible use of such events:


			Redirecting to a web site:
			Imports AddinExpress.IE
			Imports IE = Interop.SHDocVw
			Private Sub IEModule_BeforeNavigate2 _
				(e As ADXIEBeforeNavigate2EventArgs) Handles MyBase.BeforeNavigate2

				If e.Url.ToString().ToLower().Contains("") Then
					e.Cancel = True
				End If
			End Sub


			Modifying the document by adding a script and a button that calls the script:
			Private Sub IEBarModule_DownloadComplete() Handles MyBase.DownloadComplete
				If ThisIsMyPage(Me.HTMLDocument.url) Then
					Dim doc As mshtml.HTMLDocument = Me.HTMLDocument
					If doc.body IsNot Nothing Then
							Dim myScript As String = _
								"function ShowAlert(){alert(""" & "Hello" & """);}"
							Dim isMyScriptCreated As Boolean = False
							Dim scripts As mshtml.IHTMLElementCollection = doc.scripts
							If scripts IsNot Nothing Then
								For i As Integer = 0 To scripts.length - 1
									Dim script As mshtml.IHTMLScriptElement = _
										TryCast(scripts.item(Nothing, i), _
									If script IsNot Nothing Then
										If script.text IsNot Nothing Then
											If script.text = myScript Then 
												isMyScriptCreated = True
											End If
										End If
									End If
									If isMyScriptCreated Then Exit For
							End If
							If Not isMyScriptCreated Then
								Dim head As mshtml.HTMLHeadElementClass = _
									TryCast(CType(doc.all.tags("head"), _
											mshtml.IHTMLElementCollection).item(Nothing, 0), _
								If head IsNot Nothing Then
									Dim script As mshtml.IHTMLScriptElement = _
										CType(doc.createElement("script"), _
									script.type = "text/javascript"
									script.text = myScript
								End If
							End If
						Catch e As InvalidCastException
							System.Windows.Forms.MessageBox.Show( _
								New MyWinApiWindow(Me.ParentHandle), _
								e.Message + vbCrLf + e.StackTrace)
						End Try

						Dim newButton As String = _
							"<input type=""button"" name=""myButton""" + _
							onClick=""ShowAlert();"" value=""Click me!"">"
						Me.HTMLDocument.body.insertAdjacentHTML("beforeEnd", newButton)
					End If
				End If
			End Sub


			Module-script two-way interaction.
			Imports System.Windows.Forms
			Imports AddinExpress.IE
			Imports System.Reflection
			Private Sub IEToolbar_DownloadComplete() Handles MyBase.DownloadComplete
				If Not ThisIsMyPage(Me.HTMLDocument.url) Then Return
					Dim scriptEngine As Object = Me.HTMLDocument.Script
						BindingFlags.SetProperty, Nothing, scriptEngine, New Object() {Me})
				Catch ex As Exception
				End Try

				'<html><head><script type="text/javascript">
				'var myAddon = null;
				'function MyFunction() 
				'    if(myAddon != null)
				'    {
				'        myAddon.MyMethod("okay");
				'    }
				'    else
				'        alert("The add-on isn't registered");
				'<input type="button" name="button1" value="Click me!" onclick="MyFunction();">
			End Sub

			Public Sub MyMethod(param As String)
				MessageBox.Show(New MyWinApiWindow(Me.ParentHandle), param)
			End Sub

Intercepting HTML events

Right-click the designer surface of the IE module and choose Add HTML Events in the context menu.

Adding the component to handle HTML events

This adds an ADXIEHTMLDocEvents component onto the module.

Intercepting events generated by HTML controls, namely anchors and buttons

In the SupportedEvents property of the component, select HTML control type(s) the events of which the component will intercept. Then add event handlers to appropriate events of the component. Every such event (OnClick is an example) provides the eventObject parameter. See how we use it in the code below:


			Intercepting clicking on HTML anchor elements.
			Private Sub AdxiehtmlDocEvents1_OnClick( _
					ByVal sender As System.Object, _
					ByVal eventObject As System.Object, _
					ByVal e As  _
						AddinExpress.IE.ADXCancelEventArgs) _
					Handles AdxiehtmlDocEvents1.OnClick
				Dim eventObj As mshtml.IHTMLEventObj2 = _
					CType(eventObject, mshtml.IHTMLEventObj2)
				Dim elem As mshtml.IHTMLElement = _
					Me.HTMLDocument.elementFromPoint(eventObj.clientX, eventObj.clientY)
				If elem IsNot Nothing Then
					If TypeOf elem Is mshtml.IHTMLAnchorElement Then
						Dim anchor As mshtml.IHTMLAnchorElement = _
							CType(elem, mshtml.IHTMLAnchorElement)
						Dim window As MyWinApiWindow = New MyWinApiWindow(Me.ParentHandle)
						If System.Windows.Forms.MessageBox.Show(window, _
								"You've clicked the link labelled '" _
								+ elem.innerText _
								+ "' and pointing to a page at " _
								+ anchor.hostname + "." + vbCrLf + vbCrLf + _
								"Do you want to continue?", _
								Me.ModuleName, MessageBoxButtons.YesNo) _
							= MsgBoxResult.No _
							e.Cancel = True
						End If
					End If
				End If
			End Sub

The MyWinApiWindow type above is a simple class implementing the System.Windows.Forms.IWin32Window interface (see MyWinApiWindow Class). The window handle for MyWindow is supplied by the ParentHandle property of the module that returns the handle (hwnd) of the tab window. Note that you must show messages in this way; otherwise your message box may be shown behind the IE window.

Intercepting keyboard shortcuts

Right-click the designer surface of the IE module and choose Add Keyboard Shortcut in the context menu.

Adding the Add Keyboard Shortcut component

This adds an ADXIEKeyboardShortcut component onto the module. Specify the keyboard shortcut in the ADXIEKeyboardShortcut.ShortcutText property of the component and handle the OnAction event. Note however that intercepting "native" Internet Explorer shortcuts is not possible. Also note that using the component requires setting the HandleShortcuts property of the module to true.


			Executing a script.
			Private Sub AdxieKeyboardShortcut1_OnAction(sender As System.Object) _
				Handles AdxieKeyboardShortcut1.OnAction

				Me.HTMLDocument.parentWindow.execScript("Alert()", Nothing)
			End Sub

Building Explorer bars <<

>> How an IE add-on works: tabs, windows, threads, instancing