Answered by:
HtmlElement.KeyPress event not firing.

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
-
it is in the HtmlDocument.Focusingevent. Read the active element property of the document in the event handler.
Visual C++ MVP- Edited by Sheng Jiang 蒋晟 Monday, April 4, 2016 8:17 PM
- Proposed as answer by Moonlight ShengMicrosoft contingent staff Wednesday, April 13, 2016 8:07 AM
- Marked as answer by Moonlight ShengMicrosoft contingent staff Thursday, April 14, 2016 8:03 AM
Monday, April 4, 2016 8:13 PM
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 -
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
-
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- Edited by Sheng Jiang 蒋晟 Monday, April 4, 2016 8:17 PM
- Proposed as answer by Moonlight ShengMicrosoft contingent staff Wednesday, April 13, 2016 8:07 AM
- Marked as answer by Moonlight ShengMicrosoft contingent staff Thursday, April 14, 2016 8:03 AM
Monday, April 4, 2016 8:13 PM