在我看来,您回答了自己的问题。如果您正在检测 SHIFT 键,则可以轻松区分大写和小写:
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);
}
还是我误解了你的问题?
更新:通过添加 32 可以轻松地将大写 ASCII 代码转换为小写 ASCII 代码,因此您需要做的就是:
<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>
更新 2:试试这个:
<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>