.NET: How to capture the keystroke events (KeyPress/KeyDown/KeyUp) in WebBrowser?

Posted by NosaLee in .NET Programming on 28-03-2013. Tags: , , , , , , , , , ,

Author: Nosa Lee
Original Address: http://www.seeksunslowly.com/dot-net-capture-keystroke-events-keypress-keydown-keyup-webbrowser
To reprint this article, please indicate the source, thank you.
_____________________________________

Sometimes, we need to process the keystroke events in the WebBrowser control when it is Design mode (such as determining the content of WebBrowser is changed). But viewed all events that supported by WebBrowser in the code editor, cannot find the KeyPress/KeyDown/KeyUp events.

In fact, WebBrowser is an IE, it is quite complex, and may be instable and uncontrollable once you embedded it in a form, if you want to intercept the keystroke events and rewrite it, it is too hard, and this may cause other problems, let it to be more uncontrollable.

Through reading MSDN documentation and test, I found it is very easy, just use the PreviewKeyDown event, this event is not occurred when user pressed the “Preview” key (because users can preview the actual effect for a designing WebBrowser, so it is easy to mislead me literally), it is just an event that occurred before KeyDown event.

In fact, all these are caused by I rarely use PreviewKeyDown event, because PreviewKeyDown is just a native event of System.Windows.Form.Control class. If you used this event ever, only need a look, will not be struggling.

Post a comment