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???
key 13 keycode is for ENTER key.
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.
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.
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.
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