Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
155
rated 0 times [  162] [ 7]  / answers: 1 / hits: 21649  / 14 Years ago, sat, june 26, 2010, 12:00:00
<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 differentiate capital A from lowercase a in onkeydown event handler method? Above algorithm fires the same keyCode value. I need to detect the capital letters when they are pressed in onkeydown.



Note: The code contains an exception for SHIFT key. Otherwise it does not allow to type capital letters. BTW, I need to use onkeydown for my trial.


More From » onkeydown

 Answers
8

It looks to me like you answered your own question. If you are detecting the SHIFT key, you can easily differentiate between capital and lowercase:



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 am I misunderstanding your question?



Update: Upper case ASCII codes can easily be converted to lower case ASCII codes by adding 32, so all you need to do is this:



<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 this:



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

[#96402] Tuesday, June 22, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jacklynr

Total Points: 542
Total Questions: 120
Total Answers: 95

Location: Cape Verde
Member since Fri, Nov 27, 2020
4 Years ago
;