Definitive way to trigger keypress events with jQuery

后端 未结 10 2311
不知归路
不知归路 2020-11-22 00:33

I\'ve read all the answers on to this questions and none of the solutions seem to work.

Also, I am getting the vibe that triggering keypress with special characters

10条回答
  •  清酒与你
    2020-11-22 00:48

    Ok, for me that work with this...

    var e2key = function(e) {
        if (!e) return '';
        var event2key = {
            '96':'0', '97':'1', '98':'2', '99':'3', '100':'4', '101':'5', '102':'6', '103':'7', '104':'8', '105':'9', // Chiffres clavier num
            '48':'m0', '49':'m1', '50':'m2', '51':'m3', '52':'m4', '53':'m5', '54':'m6', '55':'m7', '56':'m8', '57':'m9', // Chiffres caracteres speciaux
            '65':'a', '66':'b', '67':'c', '68':'d', '69':'e', '70':'f', '71':'g', '72':'h', '73':'i', '74':'j', '75':'k', '76':'l', '77':'m', '78':'n', '79':'o', '80':'p', '81':'q', '82':'r', '83':'s', '84':'t', '85':'u', '86':'v', '87':'w', '88':'x', '89':'y', '90':'z', // Alphabet
            '37':'left', '39':'right', '38':'up', '40':'down', '13':'enter', '27':'esc', '32':'space', '107':'+', '109':'-', '33':'pageUp', '34':'pageDown' // KEYCODES
        };
        return event2key[(e.which || e.keyCode)];
    };
    
    var page5Key = function(e, customKey) {
        if (e) e.preventDefault();
        switch(e2key(customKey || e)) {
            case 'left': /*...*/ break;
            case 'right': /*...*/ break;
        }
    };
    
    $(document).bind('keyup', page5Key);
    
    $(document).trigger('keyup', [{preventDefault:function(){},keyCode:37}]); 
    

提交回复
热议问题