Javascript keyup / keydown in .NET WebBrowser Control

I have a .NET WebBrowser control that I use to display some of the heavy pages that I wrote. Pages use YUI and have been designed to be portable.

I just found that although I can capture keypress in javascript, I cannot capture keyup or keydown in javascript . This does not allow me to connect ESC, CTRL + A, UP, RIGHT, TAB, for example.

I understand that I can grab keys in .NET and that there are hacks for some of them. For example, Document.ExecCommand ("SelectAll", .., ..) for CTRL + A. --- By the way, I still cannot get SendKeys.Send ("{TAB}") to work on the --- tab. I understand that I can use .NET to execute a function that processes the UP arrow, but for reasons of portability and best practice, I really don't want to do this.

Can someone explain why I cannot capture keyup / keydown events in javascript or suggest a workaround?

Thanks!

+6
javascript c # browser keyboard
source share
3 answers

Just ran into this problem with ATL web browser management (CAxHost). Not sure how to do this in winforms, but you need to execute QueryInterface for IOleInPlaceActiveObject from the browser object and then call IOleInPlaceActiveObject :: TranslateAccelerator before calling TranslateMessage in your message loop. I think in winforms this means listing the ActiveXInstance property for IOleInPlaceActiveObject (you can probably find the interface definition from pinvoke.net)

This should only be to allow the controller to execute its accelerators (e.g. ctrl-p for printing, F5 for updating, etc.), but it seems that keydown events also disable this call. If you want to block some of these accelerators, I think you can do this by running IDocHostUIHandler :: TranslateAccelerator in the AxA node of the browser and checking the Ole command check to disable it.

+2
source share

I tried to download this example from yui (http://developer.yahoo.com/yui/examples/container/keylistener_clean.html) in a winForm application in which I reset the WebBrowser control and everything works fine. This example uses the keyup event, so I believe that it suits your case.

You might want to check what mode the web browser is controlled at http://blogs.msdn.com/ie/archive/2008/03/18/webbrowser-control-rendering-modes-in-ie8.aspx

Marco

+1
source share

I wrote you some code to show how to capture the arrow key press in JavaScript. I tested this in IE and FireFox, so everything should be fine ...

<input id="Text1" type="text" onkeydown="keyPress(event)" /> <script type="text/javascript"> function keyPress(e) { var textBox = document.getElementById('Text1'); var keynum; if (window.event) // IE keynum = e.keyCode; if (e.which) // Other browser keynum = e.which; switch (keynum) { case 38: textBox.value = 'Up Arrow'; break; case 37: textBox.value = 'Left Arrow'; break; case 40: textBox.value = 'Down Arrow'; break; case 39: textBox.value = 'Right Arrrow'; break; default: textBox.value = 'Another key'; break; } } </script> 

Hope this helps :)

0
source share

All Articles