Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何绑定组合键ctrl+;x+;在jquery中返回_Javascript_Jquery - Fatal编程技术网

Javascript 如何绑定组合键ctrl+;x+;在jquery中返回

Javascript 如何绑定组合键ctrl+;x+;在jquery中返回,javascript,jquery,Javascript,Jquery,有没有办法在jquery(或javascript)中捕获组合键ctrl+x+return,这样,如果用户按下这个组合键,就会调用一个函数。我尝试使用jquery热键插件,但没有成功。您可以使用按键事件对象的属性 $(document).keypress(function(e) { if(e.ctrlKey) { // do code to test other keys } }); 此外,shift、alt保留键具有属性,但enter键具有键代码13 是否有任何

有没有办法在jquery(或javascript)中捕获组合键ctrl+x+return,这样,如果用户按下这个组合键,就会调用一个函数。我尝试使用jquery热键插件,但没有成功。

您可以使用按键事件对象的属性

$(document).keypress(function(e) {
    if(e.ctrlKey) {
        // do code to test other keys
    }
});
此外,shift、alt保留键具有属性,但enter键具有键代码13


是否有任何理由不能尝试其他组合-例如ctrl-alt-x?Enter键通常用于触发表单提交和网页上的其他事件。

使用全局布尔数组
var keys=[]
检查是否按下了键。然后使用以下功能添加全局热键:

$("body").bind("keydown",keyDown);

function keyDown(e){
    if((e.ctrlKey)&&(e.keyCode == 88)&&(e.keyCode == 13)){
        alert("Keys down are Ctrl + x + Return");
    }
}
window.addGlobalHotkey = function(callback,keyValues){
    if(typeof keyValues === "number")
        keyValues = [keyValues];

    var fnc = function(cb,val){
        return function(e){
            keys[e.keyCode] = true;
            executeHotkeyTest(cb,val);
        };        
    }(callback,keyValues);
    window.addEventListener('keydown',fnc);
    return fnc;
};
如您所见,它向
'keydown'
事件添加了一个新的侦听器。此侦听器将首先在
keys
true中设置相应的值,然后执行测试,无论给定的
keyValues
当前是否为true。请注意,您不能删除
键[e.keyCode]=true
并将其放入另一个侦听器中,因为这可能会导致错误的回调顺序(首先是热键测试,然后是键映射)。
executeHotkeyTest
本身也非常简单:

window.executeHotkeyTest = function(callback,keyValues){
    var allKeysValid = true;

    for(var i = 0; i < keyValues.length; ++i)
        allKeysValid = allKeysValid && keys[keyValues[i]];

    if(allKeysValid)
        callback();
};
现在,您可以使用
addGlobalHotkey(回调[13,17,88])向ctrl+x+enter添加热键。

您可以使用全局
[[callback1,values1],[callback2,values2],…]数组,而不是为每个热键添加侦听器

重要注意事项:在IE早期版本9中,您必须使用
attachEvent
而不是
addEventListener
。因为您已经在使用jQuery,所以可以使用(…)
.keydown
来代替它。

您可能会发现使用是一个更好的解决方案。它的水坝使用起来很简单。这里是

此外,如果要在x或enter之前强制按ctrl键,可以改为按ctrl键

KeyboardJS.on('ctrl > x + enter', function() {
    //do stuff on press
}, function() {
    //do stuff on release
});

只需像这样键入:

    document.getElementById('yourelementid').onkeydown = function(){
       if(event.ctrlKey===true && event.keyCode==88){
           //your code goes here
       }
    }
//no depedency needs. pretty straight-forward

我发现ctrl-return组合键可能有问题,尤其是跨浏览器的组合键——在这里进一步阅读:我尝试了jquery热键和捕鼠器,但都不起作用。。。好像有段时间没更新了。这是最近更新和工程!
KeyboardJS.on('ctrl + x + enter', function() {
    //do stuff on press
}, function() {
    //do stuff on release
});
KeyboardJS.on('ctrl > x + enter', function() {
    //do stuff on press
}, function() {
    //do stuff on release
});
    document.getElementById('yourelementid').onkeydown = function(){
       if(event.ctrlKey===true && event.keyCode==88){
           //your code goes here
       }
    }
//no depedency needs. pretty straight-forward