如何在jquery中创建用于调用事件的快捷键? [英] How to create shortcut key for calling an event in jquery?

查看:50
本文介绍了如何在jquery中创建用于调用事件的快捷键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在jQuery中创建用于调用事件的快捷键(就像我按 Alt + A 然后调用按钮单击功能。但是如果 Alt + V + A )。

How to create shortcut key for calling an event in jQuery (Like if I press Alt + A then call a button click function. But not if Alt + V + A).

推荐答案

我不知道这是不是最好的解决方案,但也许有帮助:

I don't know if this is the best solution, but maybe helps:

警告:这不是战斗测试解决方案

var pressedKeys = [];

document.addEventListener('keydown', function(e) {  
    if(e.altKey){
        var idx = pressedKeys.indexOf(e.which);
        if(idx < 0) pressedKeys.push(e.which);
    }
});

document.addEventListener('keyup', function(e) {
    // 65 means A
    if (e.altKey && e.which == 65){
        if(pressedKeys.length === 2)
            console.log("Alt + A shortcut combination was pressed");  
    }

    var idx = pressedKeys.indexOf(e.which);
    if(idx > -1) pressedKeys.splice(idx, 1);
});

您可以看到上面的代码, 这里 代码集上

You can see above code in action, here on codepen

这篇关于如何在jquery中创建用于调用事件的快捷键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆