3

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?

Jacob Bundgaard
  • 943
  • 11
  • 29

2 Answers2

0

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.

Eric Legault
  • 5,706
  • 2
  • 22
  • 38
  • Thanks. Unfortunately, I'm not trying to wire up everything from .NET. I just want to be able to handle keyboard events from my JavaScript code. If possible, see the minimal example. Adding a `Panel` control around the `WebBrowser` seems to allow the very first keyboard event to go through, but not subsequent ones. It's perplexing. – Jacob Bundgaard Jun 25 '15 at 07:01
  • Well, that's weird. It's also strongly recommended that you don't embed the WebBrowser control in a UserControl. However, you have no choice but to use a UserControl for VSTO Task Panes - and I have inherited a project that uses that combination just fine. But when rebuilding that project using Add-in Express, I had to STOP using a UserControl with a WebBrowser control in order to get things like double-clicks to select text in a web page (amongst other things) to work. There is a litany of strange behaviour with this control. – Eric Legault Jun 25 '15 at 14:49
  • Hmm... Maybe I'll have to stop using the WebBrowser control altogether and switch to something like Chromium Embedded Framework. – Jacob Bundgaard Jun 26 '15 at 05:53
  • Thanks for the Chromium tip. Another alternative is Gecko. – Eric Legault Jun 26 '15 at 14:29
  • You're welcome. And yes, I see [GeckoFX](https://bitbucket.org/geckofx/geckofx-33.0) is a possibility as well. I'll leave the question open for a while to see if someone has a quick fix for WebControl. – Jacob Bundgaard Jun 26 '15 at 19:13
  • Hi mate, did you manage to find a solution to this problem? I have been trying to do the same and haven't been able to achieve anything as of yet.http://stackoverflow.com/questions/29920616/enable-tab-key-in-my-web-browser-control – Neophile Mar 17 '16 at 16:33
  • Hi @JacobBundgaard, have you found a solution? does CEF work better? – Cloud Xu Aug 04 '16 at 22:29
  • @CloudXu: I haven't found a solution for the WebBrowser control. I've evaluated a number of CEF packages and [EO.WebBrowser](http://www.essentialobjects.com/Products/WebBrowser/Default.aspx) looks the best. – Jacob Bundgaard Aug 05 '16 at 08:16
0

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.

Cloud Xu
  • 3,267
  • 2
  • 15
  • 14
  • Unfortunately, in the minimal example I've uploaded, that doesn't seem to do the trick in the browser in the task pane. – Jacob Bundgaard Aug 25 '16 at 13:51
  • It does work for me with my single example. Just one catch is when the focus is in a cell (edit mode), it will not work. You just need to exit the edit mode (by ESC i think), then the webview is operational. – Cloud Xu Aug 26 '16 at 22:24