Is the shiftkey held down in JavaScript

后端 未结 3 1682
面向向阳花
面向向阳花 2020-12-19 07:15

I have written a JS function that only allow numbers to be entered. A copy of that function is below:

function NumbersOnly(e) {
    var evt = e || window.eve         


        
相关标签:
3条回答
  • 2020-12-19 07:54

    use keypress for holding any key isntead of keydown

    0 讨论(0)
  • 2020-12-19 08:10

    Use event.key instead of charCode. No more magic numbers!

    function onEvent(event) {
        const key = event.key; // "a", "1", "Shift", etc.
        if (isFinite(key)) { // Is number
            // Do work
        }
    };
    

    Mozilla Docs

    Supported Browsers

    0 讨论(0)
  • 2020-12-19 08:18

    You can check if shift key is pressed using :

    if(evt.shiftKey) {
     ...  //returns true if shift key is pressed
    
    0 讨论(0)
提交回复
热议问题