Reputation: 2403
I'm struggling with the WebBrowser
control (both in Winforms and WPF). Basically I want to achieve the same behavior I got with a RTF
editor: Handling some kind of OnTextInput
event in order to get the last typed character for every keystroke.
I mean the textual characters, not Control, Alt, F5, Enter, ... that can be captured with the Keydown/Keyup
events.
Any help? Thanks in advance.
Upvotes: 1
Views: 138
Reputation: 125342
You can hanlde KeyPress
event of this.webBrowser1.Document.Body
:
private void Form1_Load(object sender, EventArgs e)
{
this.webBrowser1.Navigate("http://www.google.com");
//Attach a handler to DocumentCompleted
this.webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//Attach a handler to Body.KeyPress when the document completed
this.webBrowser1.Document.Body.KeyPress += Body_KeyPress;
}
void Body_KeyPress(object sender, HtmlElementEventArgs e)
{
//handle the event, for example show a message box
MessageBox.Show(((char)e.KeyPressedCode).ToString());
}
Note:
e.ReturnValue = false;
based on some criteria if you need.KeyUp
and KeyDown
the same wayUpvotes: 1