views:

36

answers:

1

I'm using Ubuntu 10 and trying out the below code in Firefox 3.6 and Chrome 5.

    $(document).ready(function(){
        $(document).bind("keypress", function(e){
            alert("Pressed");
        });
    });

Surprisingly the above code works well in FF3.6. i.e. for every key pressed, I'm getting an alert box.

But in Chrome, i'm seeing a different behavior. For example, I'm not getting the alert box when I press page up/down, arrow up/down/left'right keys (unfortunately, these are the key events that I desperately want to track).

Does anyone why Chrome is not binding the 'keypress' event to certain keys, like page up/down, arrow left/right?

+2  A: 

The onkeypress event was originally a proprietary event for Internet Explorer, later included in other browsers but with differing implementations. Chrome/Safari stayed true to the Microsoft implementation, for which the documentation states:

As of Microsoft Internet Explorer 4.0, the onkeypress event fires and can be canceled for the following keys:

Letters: A - Z (uppercase and lowercase)
Numerals: 0 - 9
Symbols: ! @ # $ % ^ & * ( ) _ - + = < [ ] { } , . / ? \ | ' ` " ~
System: ESC, SPACEBAR, ENTER

For other keys, you should bind to keydown or keyup.

The Quirksmode.org compatibility reference states:

When the user presses special keys such as the arrow keys, the browser should NOT fire keypress events.

Andy E