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>
jahroy
  • 22,322
  • 9
  • 59
  • 108
TGH
  • 38,769
  • 12
  • 102
  • 135
  • Use `e.which` to get the keycode - http://api.jquery.com/event.which/ – Ian Apr 02 '13 at 18:30
  • 1
    Check out [this](http://stackoverflow.com/a/5871443/778118)... As it states, keycodes and ASCII values are **NOT** the same. – jahroy Apr 02 '13 at 18:31

1 Answers1

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));
})
Justice Erolin
  • 2,869
  • 20
  • 19
  • 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 02 '13 at 18:36