获取在javascript中双击时关闭的键

时间:2015-01-25 09:44:46

标签: javascript jquery double-click

我想知道在元素上触发双击事件时,键被击落(保持和按下)是什么。

事件处理程序允许我获取alt,shift,meta和ctrl键。如果我想检测是否' x'双击时被击落......或任何其他字母或数字。

3 个答案:

答案 0 :(得分:1)

如果要检测ctrl,alt或shift键,它们会在传递给您的事件对象上公开。

$(document).on('dblclick', function(e){
    /*
     * here you could use e.altKey, e.ctrlKey and e.shiftKey - all of them
     * are bools indicating if the key was pressed during the event.
     */
});

如果你想检测一个不同的密钥,那么omar-ali的答案似乎是正确的。

答案 1 :(得分:0)

一种可能性就是这样做,88 =字母x ..但是......有更好的方法。

$(document).on('keydown','body',function(e) {
        //console.log(e.keyCode);
        if(e.keyCode==88)
            keyed = true;

    });

    $(document).on('keyup','body',function(e) {
        if(e.keyCode==88)
            keyed = false;
    });

    $(document).on('dblclick','body',function(e) {
        if(keyed==true)
            alert('yes');
        keyed=false;
    });

答案 2 :(得分:0)

您必须将keycode存储到keyup事件,并在双击事件时引用当前值。

var heldKey;
$(document).on({ 
    'keydown' : function(e) {        
        heldKey = e.which || e.keyCode;
    },
    'keyup': function(e) { 
        heldKey = undefined;
    },
    'dblclick': function(e){
        console.log(String.fromCharCode(heldKey));
    }
});