Keycodes for the Numeric Keypad

HugMyster picture HugMyster · Feb 13, 2018 · Viewed 7.2k times · Source

I was looking at the Stack Overflow questions to see if there was a keycode for the number keypad (the keys on the right of the keyboard). A list of useful answers was given but also confusing. Apparently Numpad-0 is 96, Numpad-1 is 97, Numpad-2 is 98, and so on.

This would be alright except that isn't 97 already allocated to capital A and 98 to capital B?

Can anyone help please?

Answer

Milney picture Milney · Feb 13, 2018

The Javascript Keycodes differ from the ASCII codes (called Character Codes in javascript) sadly, because ASCII doesn't differentiate between a 1 from the top of the keyboard and a 1 from the number pad - they are the same character. Likewise the javascript key codes don't differentate between a capital and lowercase letter, because they are the same key...

The ASCII codes are shown here: http://www.asciitable.com/index/asciifull.gif

and the Javascript key codes here: https://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

This page (https://www.w3schools.com/jsref/event_key_keycode.asp) describes the difference;

The difference between the two code types:

Character codes - A number which represents an ASCII character

Key codes - A number which represents an actual key on the keyboard

These types do not always mean the same thing; for example, a lower case "w" and an upper case "W" have the same keyboard code, because the key that is pressed on the keyboard is the same (just "W" = the number "87"), but a different character code because the resulting character is different (either "w" or "W", which is "119" or "87") - See "More Examples" below to better understand it.