Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get keyCode of character if SHIFT is used?

If a character is entered through keyboard using the SHIFT key then the keydown or keyup event only can trace the keyCode of SHIFT key i.e. 16.

Then how to trace the keyCode of the character actually printed?

I am using the following code-

onkeyup='alert(event.keyCode)';// Always shows a message 16(keyCode of SHIFT) irrespective of the actual character

or

onkeydown='alert(event.keyCode)';// Always shows a message 16(keyCode of SHIFT) irrespective of the actual character

Then how to get keyCode of the actual character printed???

like image 940
Rajesh Paul Avatar asked Sep 14 '13 17:09

Rajesh Paul


People also ask

What is e keyCode === 13?

key 13 keycode is for ENTER key.

How do I get keyCode characters?

One way to charCodeAt() function convert character to keycode character, Another fromCharCode() function convert unicode keycode to character. Key codes for letters are unique numbers for any characters including Unicode characters.


2 Answers

You can see the property shiftKey of the passed event object.

Take a look at this Fiddle

When you press Shift you can see the keyCode of it and shiftKey property true. Press any button together, i.e. Shift+A and the console outputs:

65
true

Which is the code of A and shiftKey property again.

like image 121
Artyom Neustroev Avatar answered Sep 20 '22 22:09

Artyom Neustroev


Got the solution

I had to use onKeyPress event which does not treat SHIFT as keypress but the resultant character instead.

Hence I can get the keyCode of the actual resultant character using onKeyPress event.

Syntax:

onkeypress='alert(event.keyCode)';

Now If I press SHIFT+A it prompts the keyCode of A i.e. 65.

like image 43
Rajesh Paul Avatar answered Sep 17 '22 22:09

Rajesh Paul