How to distinguish upper and lower case letters in the onkeydown event handler method

<html> <head> <title>Test</title> <script type="text/javascript"> function showChar(e) { if (e.keyCode != 16) { alert( "keyCode: " + e.keyCode + "\n" + "SHIFT key pressed: " + e.shiftKey + "\n" ); } } </script> </head> <body onkeydown="showChar(event);"> <p>Press any character key, with or without holding down the SHIFT key.<br /></p> </body> </html> 

How can I distinguish capital A from lowercase a in the onkeydown event handler method? Above, the algorithm runs the same key code value. I need to detect capital letters when they are pressed in onkeydown.

Note. The code contains an exception for the SHIFT key. Otherwise, it does not allow capitalization. By the way, I need to use onkeydown for my test.

+6
javascript onkeydown
source share
3 answers

It seems to me that you answered your question. If you find the SHIFT key, you can easily distinguish between capital and lowercase letters:

 if(e.shiftKey){ alert("You pressed a CAPITAL letter with the code " + e.keyCode); }else{ alert("You pressed a LOWERCASE letter with the code " + e.keyCode); } 

Or I do not understand your question?

Update . Uppercase ASCII codes can easily be converted to lowercase ASCII codes by adding 32, so all you have to do is the following:

 <html> <head> <title>Test</title> <script type="text/javascript"> function showChar(e){ if(e.keyCode!=16){ // If the pressed key is anything other than SHIFT if(e.keyCode >= 65 && e.keyCode <= 90){ // If the key is a letter if(e.shiftKey){ // If the SHIFT key is down, return the ASCII code for the capital letter alert("ASCII Code: "+e.keyCode); }else{ // If the SHIFT key is not down, convert to the ASCII code for the lowecase letter alert("ASCII Code: "+(e.keyCode+32)); } }else{ alert("ASCII Code (non-letter): "+e.keyCode); } } } </script> </head> <body onkeydown="showChar(event);"> <p>Press any character key, with or without holding down the SHIFT key.<br /></p> </body> </html> 

Update 2: Try the following:

 <html> <head> <title>Test</title> <script type="text/javascript"> function showChar(e){ if(e.keyCode!=16){ // If the pressed key is anything other than SHIFT c = String.fromCharCode(e.keyCode); if(e.shiftKey){ // If the SHIFT key is down, return the ASCII code for the capital letter alert("ASCII Code: "+e.keyCode+" Character: "+c); }else{ // If the SHIFT key is not down, convert to the ASCII code for the lowecase letter c = c.toLowerCase(c); alert("ASCII Code: "+c.charCodeAt(0)+" Character: "+c); } } } </script> </head> <body onkeydown="showChar(event);"> <p>Press any character key, with or without holding down the SHIFT key.<br /></p> </body> </html> 
+7
source share

I searched quite a bit on this issue, as I wrote code to restrict the input element (using the keyPress event) to alpha characters only. I used "character codes between 65 and 90" and could not enter lowercase letters in the field.

I finally found that JavaScript uses ASCII codes for this, so I just added "and also between 97 and 122" and alt! I can enter upper and lower case letters. To wit:

 function alphaOnly(e) { 'use strict'; if (!e) { // compensates for IE key code reporting. e = window.event; } var charCode = (e.which) ? e.which : event.keyCode; if ((charCode >= 65 && charCode <= 90) || (charCode >= 97 && charCode <= 122)) { return true; } else { return false; } } 
+1
source share

More recent and much cleaner: use event.key . There are no arbitrary numbers!

 node.addEventListener('keydown', function(event) { const key = event.key; // "a", "1", "Shift", etc. if (/^[az]$/i.test(key)) { // or if (key.length === 1 && /[az]/i.test(key)) const isCapital = event.shiftKey; } }); 

Mozilla Docs

Supported Browsers

0
source share

All Articles