Using e.keyCode || e.which; how to determine the difference between lowercase and uppercase?

后端 未结 5 1928
星月不相逢
星月不相逢 2020-12-20 20:32

I am using e.keyCode || e.which; to determine which key was pressed, but I am getting 65 for both a and A why is this happening and ho

相关标签:
5条回答
  • 2020-12-20 21:12

    This script is useful for small and capital letters press

    <form>
        Char: <input type="text" id="char" size="15" /> Keycode: <input type="text" id="unicode" size="15" />
        </form>
    
        <script type="text/javascript">
    
        var charfield=document.getElementById("char")
        charfield.onkeypress=function(e){
        var e=window.event || e
        var keyunicode=e.charCode || e.keyCode
        document.getElementById("unicode").value=keyunicode
        }
    
        </script>
    
    0 讨论(0)
  • 2020-12-20 21:13

    Whether it's 'a' or 'A', 65 is the result of the key pressed on the keyboard and it's always 65 for that key.

    The event will only specify which key is pressed and not its value; those are two separate things. You can test for event.shiftKey along with the key that you're looking for, but I don't believe that will handle the scenario where Caps Lock is enabled.

    0 讨论(0)
  • 2020-12-20 21:19

    keyCode won't indicate which character was input.
    To truly find the character last entered by the user you will need to examine the value of the input and find the last character.

    0 讨论(0)
  • 2020-12-20 21:23

    just use e.which in jquery. They normalize this value for all browsers.

    Additionally you can check for e.shiftKey.

    0 讨论(0)
  • 2020-12-20 21:23

    This is spectacularly cross-browser-broken in vanilla JavaScript, which is why you should use Josiah Ruddell's solution. See this article for more than you want to know.

    0 讨论(0)
提交回复
热议问题