Question How to use the OnKeyUp method for WebBrowser?

pisceswzh

Well-known member
Joined
Mar 19, 2007
Messages
96
Programming Experience
1-3
According to MSDN, the WebBrowser has a protected method called OnKeyUp. I want to use this method, anyone can tell me how to use a protected method? Thanks.
 
KeyUp event is not supported by the WebBrowser control, and the inherited OnKeyUp will be useless. To handle keyboard events for the hosted document grab the body element when document is loaded and handle it's KeyUp event. Sample:
VB.NET:
Private WithEvents webel As HtmlElement

Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    If Me.WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
        webel = Me.WebBrowser1.Document.Body
    End If
End Sub

Private Sub webel_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.HtmlElementEventArgs) Handles webel.KeyUp
    Me.Text &= Chr(e.KeyPressedCode)
End Sub
 
Thanks, JohnH.

I just discovered that the PreviewKeyDown event of a WebBrowser seems to work the same as KeyUp.
 
hmm, interesting, it raises the event twice for each key down though, if not IsInputKey is set True.
 
Back
Top