jQuery Event Keypress: Which key was pressed?

后端 未结 24 2093
半阙折子戏
半阙折子戏 2020-11-21 16:48

With jQuery, how do I find out which key was pressed when I bind to the keypress event?

$(\'#searchbox input\').bind(\'keypress\', function(e) {});


        
相关标签:
24条回答
  • 2020-11-21 16:48

    Witch ;)

    /*
    This code is for example. In real life you have plugins like :
    https://code.google.com/p/jquery-utils/wiki/JqueryUtils
    https://github.com/jeresig/jquery.hotkeys/blob/master/jquery.hotkeys.js
    https://github.com/madrobby/keymaster
    http://dmauro.github.io/Keypress/
    
    http://api.jquery.com/keydown/
    http://api.jquery.com/keypress/
    */
    
    var event2key = {'97':'a', '98':'b', '99':'c', '100':'d', '101':'e', '102':'f', '103':'g', '104':'h', '105':'i', '106':'j', '107':'k', '108':'l', '109':'m', '110':'n', '111':'o', '112':'p', '113':'q', '114':'r', '115':'s', '116':'t', '117':'u', '118':'v', '119':'w', '120':'x', '121':'y', '122':'z', '37':'left', '39':'right', '38':'up', '40':'down', '13':'enter'};
    
    var documentKeys = function(event) {
        console.log(event.type, event.which, event.keyCode);
    
        var keycode = event.which || event.keyCode; // par exemple : 112
        var myKey = event2key[keycode]; // par exemple : 'p'
    
        switch (myKey) {
            case 'a':
                $('div').css({
                    left: '+=50'
                });
                break;
            case 'z':
                $('div').css({
                    left: '-=50'
                });
                break;
            default:
                //console.log('keycode', keycode);
        }
    };
    
    $(document).on('keydown keyup keypress', documentKeys);
    

    Demo : http://jsfiddle.net/molokoloco/hgXyq/24/

    0 讨论(0)
  • 2020-11-21 16:50

    Use event.key and modern JS!

    No number codes anymore. You can check key directly. For example "Enter", "LeftArrow", "r", or "R".

    const input = document.getElementById("searchbox");
    input.addEventListener("keypress", function onEvent(event) {
        if (event.key === "Enter") {
            // Submit
        }
        else if (event.key === "Q") {
            // Play quacking duck sound, maybe...
        }
    });
    

    Mozilla Docs

    Supported Browsers

    0 讨论(0)
  • 2020-11-21 16:50
    $(document).bind('keypress', function (e) {
        console.log(e.which);  //or alert(e.which);
    
    });
    

    you should have firbug to see a result in console

    0 讨论(0)
  • 2020-11-21 16:50

    Try this:

    jQuery('#myInput').keypress(function(e) {
        code = e.keyCode ? e.keyCode : e.which;
        if(code.toString() == 13) {
            alert('You pressed enter!');
        }
    });
    
    0 讨论(0)
  • 2020-11-21 16:51
    $(document).ready(function(){
        $("#btnSubmit").bind("click",function(){$('#'+'<%=btnUpload.ClientID %>').trigger("click");return false;});
        $("body, input, textarea").keypress(function(e){
            if(e.which==13) $("#btnSubmit").click();
        });
    });
    

    Hope this may help you!!!

    0 讨论(0)
  • 2020-11-21 16:51

    Here is an at-length description of the behaviour of various browsers http://unixpapa.com/js/key.html

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