Come distinguere le lettere maiuscole da quelle inferiori a metodo del gestore dell'evento onKeyDown

StackOverflow https://stackoverflow.com/questions/3125727

  •  30-09-2019
  •  | 
  •  

Domanda

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

Come posso capitale differenziare A dal minuscolo a in metodo del gestore dell'evento onKeyDown? algoritmo di cui sopra spara lo stesso valore keyCode. Ho bisogno di rilevare le lettere maiuscole quando vengono premuti in onkeydown.

Nota: Il codice contiene un'eccezione per tasto SHIFT. In caso contrario, non permette di digitare le lettere maiuscole. A proposito, ho bisogno di usare onkeydown per il mio processo.

È stato utile?

Soluzione

Sembra a me come si risposto alla tua domanda. Se si sta rilevando il tasto SHIFT, si può facilmente distinguere tra capitale e minuscole:

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);
}

O sto equivoco tua domanda?

Aggiornamento: Alta codici ASCII caso possono essere facilmente convertiti per abbassare i codici ASCII caso con l'aggiunta di 32, quindi tutto quello che dovete fare è questo:

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

Aggiornamento 2: Prova questo:

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

Altri suggerimenti

più recente e molto più pulito: event.key uso. Non più arbitraria codici numerici!

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

Browser supportati

Ho fatto un bel po 'di ricerca su questa questione, mentre stavo scrivendo il codice per limitare un elemento di input (utilizzando l'evento keyPress) ai soli alfa caratteri. Stavo usando "i codici di carattere tra i 65 ei 90" e non potevo entrare lettere minuscole nel campo.

Alla fine ho scoperto che JavaScript sta usando codici ASCII per questo, quindi ho solo aggiunto "e anche tra il 97 e il 122" e viola! Sono in grado di entrare entrambe le lettere maiuscole e minuscole. Vale a dire:

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;
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top