Search code examples
javascriptjqueryspecial-characterskeycode

Get Character value from KeyCode in JavaScript... then trim


This is what I have now:

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

If the e.keyCode may not be an ASCII character (Alt, backspace, del, arrows, etc.)... I would now need to trim these values from value somehow (preferably programmatically - not with lookup tables).

I'm using jQuery.

I must use the keydown event. keyPress doesn't activate for certain keys I need to capture (Esc, del, backspace, etc.).

I cannot use setTimeout to get the input's value. setTimeout(function(){},0) is too slow.


Solution

  • Maybe I didn't understand the question correctly, but can you not use keyup if you want to capture both inputs?

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