0

I am a bit confused by the character codes returned by the keyCode property on the keydown event. I was expecting the codes to align with the ascii codes. A lot of them seem to do, but I am getting unexpected results for some: Examples are the delete keys and the '.' key

Using the bellow code: I get the following codes back: DeleteKey: 46 (ASCII val = 127)

DelKey:110

'.' : 190 - which isn't even in the default ascii table.

It seems like the delete key maps to the Ascii code for the '.' key

I am using IE 9 to do the testing.

Is it a misunderstanding on my part that these are supposed to map to the decimal entries in the ascii table?

<script type="text/javascript">

    $(document).ready(function () {
        $("body").live("keydown", function (e) {
            console.log(e.keyCode);
        });
    });


</script>

<div id="parent">
<span id="target">Test</span>
</div>
2
  • Use e.which to get the keycode - api.jquery.com/event.which
    – Ian
    Apr 2, 2013 at 18:30
  • 1
    Check out this... As it states, keycodes and ASCII values are NOT the same.
    – jahroy
    Apr 2, 2013 at 18:31

1 Answer 1

0

I don't think that keyCode and ASCII are the same.

To get the value of the keyCode, you can use

$("body").live("keydown",function(e){
    console.log(String.fromCharCode(e.keyCode));
})
1
  • Yeah, I'm using this for display purposes, but I have to act on the keyCodes in some cases. I will look at the mapped values in the link provided by @jahroy
    – TGH
    Apr 2, 2013 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.