Skip to content Skip to sidebar Skip to footer

How To Create Shortcut Key For Calling An Event In Jquery?

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).

Solution 1:

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

Warning: this is not battle tested solution

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 Aif (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

Post a Comment for "How To Create Shortcut Key For Calling An Event In Jquery?"