views:

1168

answers:

2

Hi all,

I have a .NET WebBrowser control that I am using to display some javascript-heavy pages that I've written. The pages use YUI and have been built in a way to be portable.

I have just discovered that while I can capture keypress in javascript, I cannot seem to capture keyup or keydown in javascript. This prevents me from hooking ESC, CTRL+A, UP, RIGHT, TAB, for example.

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

Can anyone explain why I am unable to capture the keyup/keydown events in javascript or suggest a workaround?

Thanks!

A: 

Hey,

I have wrote you a bit of code to show how to capture arrow key press in JavaScript. I have tested this in IE and FireFox so it should be okay...

<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 it helps :)

Chalkey
A: 

Hi,

I tried to load this example from yui (h ttp://developer.yahoo.com/yui/examples/container/keylistener_clean.html) in a winForm Application in which I dropped a WebBrowser Control and everything works fine. The example uses the keyup event so I think it fits your case.

You might want to check in which mode you WebBrowser control is running http://blogs.msdn.com/ie/archive/2008/03/18/webbrowser-control-rendering-modes-in-ie8.aspx

Marco

crossy