javascriptdom-events

Get character typed, cross-browser


Simple question -- Does anyone know of a reliable cross-browser function to get the character typed from a keydown event? I can write one from the quirksmode grid but would rather not re-invent the wheel for something so simple yet so non-standard.

Let me clarify:

There is no way to do this simply using event.keyCode or event.which. The overall logic is something like:

It's not a simple solution, which is why I'm looking for a pre-made one :)


Solution

  • Are you willing to use jQuery?

    $("input").bind("keydown",function(e){ var value = this.value + String.fromCharCode(e.keyCode); }
    

    I believe that switching to the keypress event would solve the issue mentioned in your comment. Would the code below meet your requirments?

    $("input").bind("keypress",function(e){ var value = this.value + String.fromCharCode(e.keyCode); }