What is the keyCode for “$”?

前端 未结 5 1925
萌比男神i
萌比男神i 2021-01-18 06:07

I am trying to disable all other characters from being entered in text input.

Since to get the $ you have to press the shift-key and the

5条回答
  •  情歌与酒
    2021-01-18 06:36

    There is no key code for $, since it's not a valid key - it has to be accessed via some kind of shifter.

    Usually, you would perform a check to see if the shifter was active, and the listen out for the keypress event on the relevant key. Something like this for a QWERTY layout in your instance:

    var shiftPressed = false;
    
    $(window).keydown(function(e) {  
        if(e.which == 16) { shiftPressed = true; }
        if(e.which == 52 && shiftPressed) 
        {  
            // Do whatever here...
        }
    });
    
    $(window).keyup(function(e) {  
        if(e.which == 16) { shiftPressed = false; }
    });
    

提交回复
热议问题