22 Nov 2009 @ 7:38 PM

Hello

Did you knew that Google Chrome does not understand keypress() on special keys like arrow keys/esc/enter/etc ?

Me neither

I was trying to get the following code to work and it didnt work in Google Chrome at all

    $(window).keypress(function(e){
        var code = (e.keyCode ? e.keyCode : e.which);
        if (code == 32 || (65 <= code && code <= 65 + 25) || (97 <= code && code <= 97 + 25)) {
           alert("haxxor");
        }
  $(window).keypress(function(el){
        var alphaCode = (el.keyCode ? el.keyCode : el.which);
        if (alphaCode == 37) {
            alert("left key");
        }
        if (alphaCode == 39) {
             alert("right key")
        }

        if ( alphaCode == 13) {
            alert ("enter key")
        }
        if ( alphaCode == 27) {
            alert ("escape key")
        }      
    });

The first function works, the second works in Firefox but not in Chrome. After much headbanging around the room i tried to replace keypress() with keydown() and surprise suprise, now it works. I really dont know what to say about this bug, works with any normal key but not with special keys. Do take notice of this bug so you dont waste 30+mins trying to figure out why this happens

Cheers

Tags

Tags: , ,

Categories: jQuery

Posted By: Dinulescu Alexandru Adrian

Last Edit: 08 Jan 2010 @ 08:43 PM

EmailPermalink

Responses to this post » (None)

You must be logged in to post a comment.