I'm using keydown event
Where I get the keycode and convert it to charcode.
But I got a problem where in keyboard is press 2
it gives 50
and charcode as 2
When I press 2
in numpad it gives keycode 98
, so when I convert charcode a
keydown and keyup provide a code indicating which key is pressed, while keypress indicates which character was entered. Using jQuery e. which you can get the key code and using String. fromCharCode you can get the specific character that was pressed (including shiftKey ).
key 13 keycode is for ENTER key.
The keydown event is fired when a key is pressed. Unlike the keypress event, the keydown event is fired for all keys, regardless of whether they produce a character value. The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered.
Finding the key codeSometimes the key code is in the vehicle manual or on a label with the lock or key. On the key. It would be an engraved or hewn in code. A code that consists of raised lettering is not a key code, it is the key blank number.
That is happening because you are using the keyCode
member, for example, a lower case 'a' and an upper case 'A' have the same keyCode, because is the same key, but a different charCode because the resulting character is different.
To get the charCode, you should use the keypress
event, and get the event.charCode
member if available, otherwise, you get the event.keyCode
which for IE, on the keypress event has the right information.
Give a look to the following example:
document.onkeypress = function (e) {
e = e || window.event;
var charCode = e.charCode || e.keyCode,
character = String.fromCharCode(charCode);
alert(character);
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With