locked
HtmlElement.KeyPress event not firing. RRS feed

  • Question

  • I cannot get the HtmlElement.KeyPress event to fire. I have tried setting Form.KeyPreview to True. I can get the Mouse events for the HtmlElement to work, so I believe the rest of the code is working, just not the KeyPress event. It just isn't firing.

    Here is my code:

    -------------------------------------------------------------------------

    Public Class Form1

    Private WithEvents WebDocument As HtmlDocument
    Private WithEvents WebElement As HtmlElement

    Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
    WebDocument = WebBrowser1.Document
    End Sub

    Private Sub WebDocument_MouseMove(sender As Object, e As HtmlElementEventArgs) Handles WebDocument.MouseMove

    Dim Elem As HtmlElement = WebDocument.GetElementFromPoint(e.MousePosition)

    If Not Elem Is Nothing Then
    WebElement = Elem
    End If
    End Sub

    Private Sub WebElement_KeyPress(sender As Object, e As HtmlElementEventArgs) Handles WebElement.KeyPress
    txtKey.Text = e.KeyPressedCode
    End Sub

    End Class

    Sunday, April 3, 2016 1:17 PM

Answers

All replies

  • Try following code

        Private WithEvents WebDocument As HtmlDocument
        Private WebElement As HtmlElement
    
        Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
            WebDocument = WebBrowser1.Document
        End Sub
    
        Private Sub WebDocument_MouseMove(sender As Object, e As HtmlElementEventArgs) Handles WebDocument.MouseMove
    
            Dim Elem As HtmlElement = WebDocument.GetElementFromPoint(e.MousePosition)
    
            If WebElement IsNot Nothing Then
                RemoveHandler WebElement.KeyPress, AddressOf WebElement_KeyPress
            End If
    
            If Not Elem Is Nothing Then
                WebElement = Elem
                AddHandler WebElement.KeyPress, AddressOf WebElement_KeyPress
            End If
        End Sub
    
        Private Sub WebElement_KeyPress(sender As Object, e As HtmlElementEventArgs)
            txtKey.Text = e.KeyPressedCode
        End Sub


    Gaurav Khanna | Microsoft .NET MVP | Microsoft Community Contributor

    Sunday, April 3, 2016 2:44 PM
  • That did not work. That is essentially the same thing I had, but with more code.

    I am successfully able to hook up other events, and they fire, and they do exactly what I expect.

    The KeyPress event is not firing.

    Sunday, April 3, 2016 9:36 PM
  • the element under mouse cursor is not necessary the one with keyboard focus.


    Visual C++ MVP

    Monday, April 4, 2016 6:09 AM
  • OK. How do I get it so that the element has the keyboard focus so that I can capture the KeyPress event?
    Monday, April 4, 2016 11:11 AM
  • you can get IHTMLDocument2::activeElement. 


    Visual C++ MVP

    Monday, April 4, 2016 6:18 PM
  • That is not enough information for me. I don't know what you mean. I am using VB, btw.
    • Edited by MaryFlaws Monday, April 4, 2016 7:28 PM
    Monday, April 4, 2016 7:28 PM
  • it is in the HtmlDocument.Focusingevent. Read the active element property of the document in the event handler.


    Visual C++ MVP


    Monday, April 4, 2016 8:13 PM