Search code examples
jquerykeypress

jQuery keypress event is giving odd characters


I have made following code, but whatever I type it will always print some odd char.

$(document).keypress(function(event) {
        var character = String.fromCharCode(event.keyCode);
        $("body").append(character);
        event.preventDefault(); 
        return false;
    });

Solution

  • keyCode is not the same as a charCode, they are different maps (And differ somewhat between browsers with arrow keys and such).

    Think about it this way, what letter is Escape, or Delete?