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

如何在 onkeydown 事件处理程序方法中区分大写A和小写a ?上述算法触发相同的 keyCode 值。我需要在 onkeydown 中按下大写字母时检测它们。

注意:代码包含 SHIFT 键的异常。否则不允许输入大写字母。顺便说一句,我需要使用 onkeydown 进行试用。

4

3 回答 3

8

在我看来,您回答了自己的问题。如果您正在检测 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>
于 2010-06-26T22:51:00.753 回答
3

更新和更清洁:使用event.key. 没有更多的任意数字代码!

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

Mozilla 文档

支持的浏览器

于 2017-09-05T18:21:50.320 回答
0

我在这个问题上做了很多搜索,因为我正在编写代码以将输入元素(使用 keyPress 事件)限制为仅限字母字符。我使用的是“65 到 90 之间的字符代码”,无法在字段中输入小写字母。

我终于发现 JavaScript 使用的是 ASCII 码,所以我只添加了“and also between 97 and 122”和 viola!我可以输入大写和小写字母。以机智:

function alphaOnly(e) {
    'use strict';
    if (!e) { // compensates for IE's 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;
    }
}
于 2016-02-03T23:51:47.570 回答