WebBrowser Control TextInput events

时间:2015-11-12 10:57:55

标签: html .net wpf winforms webbrowser-control

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.

1 个答案:

答案 0 :(得分:1)

您可以KeyPress 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()); } 事件。

e.ReturnValue = false;

注意:

  • 它不会根据需要处理非输入键。
  • 如果需要,您还可以根据某些条件设置KeyUp来取消输入。
  • 您也可以同样方式处理其他重要事件,例如KeyDown-(void)buttonAction:(UIButton*)sender{ for(UIView* view in self.view.subviews){ if([view isKindOfClass:[UIButton class]] && view != sender){ UIButton *btn = (UIButton *)view; [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; // set color whichever you want } else{ [sender setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; // set color which ever you want } } }