How to enable keyboard events in WebBrowser control in Office custom task pane

809 views Asked by At

I have a WebBrowser control shown in a custom task pane in an Microsoft Office Application-level add-in created in Visual Studio. The web page shown in the WebBrowser doesn't receive keyboard events that can be handled by JavaScript code, such as KeyUp. The same page shown in a comparable WebBrowser in a Form does receive keyboard events. The WebBrowser control itself doesn't seem to expose any events related to keyboard input, and I don't seem to be able to handle keyboard events by adding event handlers to the UserControl added as a custom task pane.

I have created a minimal example – a Visual Studio solution – which recreates the problem.

Is there any way to pass on keyboard events to the web page?

2

There are 2 answers

8
Eric Legault On

Yup, you can do it. You'll have to hook into the HTML DOM events (like onclick, onmouseover, etc.). Take a look here: http://www.w3schools.com/jsref/dom_obj_event.asp.

And there's an example here: http://www.codeproject.com/Articles/547451/WebBrowser-Element-Events-and-Values

I think you'll have to add a reference to the MSHTML library. BTW, put the WebBrowser control in a Panel control or it will act goofy - especially with keyboard events.

2
Cloud Xu On

Actually I found a solution is to use the WebBrowser under Excel namespace

https://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.controls.webbrowser.aspx

Obviously it is subclassing the windows form WebBrowser and fix the keyboard issues by handing some windows message manually.