Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keycode on keydown event

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

like image 738
Santhosh Avatar asked Oct 30 '09 05:10

Santhosh


People also ask

How do you unlock characters in Keydown event?

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 ).

What is e KeyCode === 13?

key 13 keycode is for ENTER key.

What does the Keydown the key do?

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.

How do I find my key code?

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.


1 Answers

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);
};
like image 144
Christian C. Salvadó Avatar answered Oct 22 '22 16:10

Christian C. Salvadó