How to know if .keyup() is a character key (jQuery)

前端 未结 7 713
遇见更好的自我
遇见更好的自我 2020-11-29 18:57

How to know if .keyup() is a character key (jQuery)

$(\"input\").keyup(function() {

if (key is a character) { //such as a b A b c 5 3 2 $ # ^ ! ^ * # ...etc         


        
7条回答
  •  萌比男神i
    2020-11-29 19:44

    I wanted to do exactly this, and I thought of a solution involving both the keyup and the keypress events.

    (I haven't tested it in all browsers, but I used the information compiled at http://unixpapa.com/js/key.html)

    Edit: rewrote it as a jQuery plugin.

    (function($) {
        $.fn.normalkeypress = function(onNormal, onSpecial) {
            this.bind('keydown keypress keyup', (function() {
                var keyDown = {}, // keep track of which buttons have been pressed
                    lastKeyDown;
                return function(event) {
                    if (event.type == 'keydown') {
                        keyDown[lastKeyDown = event.keyCode] = false;
                        return;
                    }
                    if (event.type == 'keypress') {
                        keyDown[lastKeyDown] = event; // this keydown also triggered a keypress
                        return;
                    }
    
                    // 'keyup' event
                    var keyPress = keyDown[event.keyCode];
                    if ( keyPress &&
                         ( ( ( keyPress.which >= 32 // not a control character
                               //|| keyPress.which == 8  || // \b
                               //|| keyPress.which == 9  || // \t
                               //|| keyPress.which == 10 || // \n
                               //|| keyPress.which == 13    // \r
                               ) &&
                             !( keyPress.which >= 63232 && keyPress.which <= 63247 ) && // not special character in WebKit < 525
                             !( keyPress.which == 63273 )                            && //
                             !( keyPress.which >= 63275 && keyPress.which <= 63277 ) && //
                             !( keyPress.which === event.keyCode && // not End / Home / Insert / Delete (i.e. in Opera < 10.50)
                                ( keyPress.which == 35  || // End
                                  keyPress.which == 36  || // Home
                                  keyPress.which == 45  || // Insert
                                  keyPress.which == 46  || // Delete
                                  keyPress.which == 144    // Num Lock
                                  )
                                )
                             ) ||
                           keyPress.which === undefined // normal character in IE < 9.0
                           ) &&
                         keyPress.charCode !== 0 // not special character in Konqueror 4.3
                         ) {
    
                        // Normal character
                        if (onNormal) onNormal.call(this, keyPress, event);
                    } else {
                        // Special character
                        if (onSpecial) onSpecial.call(this, event);
                    }
                    delete keyDown[event.keyCode];
                };
            })());
        };
    })(jQuery);
    

提交回复
热议问题