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値を起動します。オンキーダウンで押されたときに大文字を検出する必要があります。

注:コードには、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);
}

それとも私はあなたの質問を誤解していますか?

更新:大文字のASCIIコードは32を追加することで小文字の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 Docs

サポートされているブラウザ

于 2017-09-05T18:21:50.320 に答える
0

(keyPressイベントを使用して)入力要素を英字のみに制限するコードを書いていたので、私はこの質問をかなり検索しました。「65から90までの文字コード」を使用していて、フィールドに小文字を入力できませんでした。

JavaScriptがこれにASCIIコードを使用していることをついに発見したので、「97から122の間」とビオラを追加しました。大文字と小文字の両方を入力できます。ウィットに:

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 に答える