Capture key events from hosted WebBrowser control

Capture key events from hosted WebBrowser control

Post by Vinayak K » Wed, 22 Feb 2006 03:16:25


The .Net 2.0's webbrowser control is neat and pretty.
But I am not able to capture Key events when the focus is on the
webbrowser control in my winform.
Options tried.
1. Wrote a class which extends this WebBrowser and override its onkeyup
event - didn't work.
2. Same as option 1 but override preprocessmessage - didn't work.
3. Same as option 1 but override WndProc - doesn't capture any key
events happening in the browser.

Nothing works.

Can someone help me here. Would be gr8 if u can solve this.

Thanks in advance,
Vin
 
 
 

Capture key events from hosted WebBrowser control

Post by Charles La » Thu, 23 Feb 2006 03:55:25

Hi Vin

The V2.0 WebBrowser control is the same old mshtml / shdocvw under the
covers, so I think you will have to do it the age old way. In the
DocumentComplete event, do the following

<snip>
Private m_DocEvents2Cookie As Integer = -1 ' make this a member
variable

...

Dim icp As ComTypes.IConnectionPoint = Nothing
Dim icpc As ComTypes.IConnectionPointContainer
Dim g As Guid

g = GetType(mshtml.HTMLDocumentEvents2).GUID

icpc = DirectCast(WebBrowser1.Document.DomDocument,
ComTypes.IConnectionPointContainer)

icpc.FindConnectionPoint(g, icp)

If m_DocEvents2Cookie <> -1 Then
icp.Unadvise(m_DocEvents2Cookie)

m_DocEvents2Cookie = -1
End If

icp.Advise(New DocumentEvents2, m_DocEvents2Cookie)
</snip>

where DocumentEvents2 is a class that you create that implements
mshtml.HTMLDocumentEvents2. The onkeyup event in your class will then fire.

HTH

Charles