<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.
I did quite a bit of searching on this question, as I was writing code to limit an input element (using the keyPress event) to alpha characters only. I was using "character codes between 65 and 90" and could not enter lowercase letters in the field.
I finally discovered that JavaScript is using ASCII codes for this, so I just added "and also between 97 and 122" and viola! I'm able to enter both upper and lower case letters. To wit:
More recent and much cleaner: use
event.key
. No more arbitrary number codes!Mozilla Docs
Supported Browsers
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:
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:
Update 2: Try this: