jQuery Capture Multiple Key Press Combinations

Share this article

You may have read a previous post on how to capture a single key press using jQuery. In this example you can capture multiple key presses in combinations. The array (arrShortCut) defines the following:

  1. a name (for reference only)
  2. a keypad code
  3. a function to execute in the variable iShortCutControlKey
if the control key activates the state” to keyup if the control key “off state”, if verified a key keydown a “no control” to “active status” look in the array if there is a preset for that key, if the function call “execShortCut” that … executes the function;
//to test use CTRL + Z = alert (2)

var arrShortCut = [{ name: 'test1', key: 15, fx: 'alert(1);' }, { name: 'test2', key: 90, fx: 'alert(2);'}];

var iShortCutControlKey = 17; // CTRL;
var bIsControlKeyActived = false;

$(document).keyup(function(e) {
    if (e.which == iShortCutControlKey) bIsControlKeyActived = false;
}).keydown(function(e) {
    if (e.which == iShortCutControlKey) bIsControlKeyActived = true;
    if (bIsControlKeyActived == true) {
        jQuery.each(arrShortCut, function(i) {
            if (arrShortCut[i].key == e.which) {
                execShortCut(arrShortCut[i].fx);
                return;
            }
        });
    }
});

function execShortCut(fx) {
    eval(fx);
}

Frequently Asked Questions about jQuery and Key Press Combinations

How can I detect multiple key press combinations in jQuery?

Detecting multiple key press combinations in jQuery involves using the keydown or keyup events. You can use the event.which property to get the key code associated with the key press. To detect multiple keys, you can use an array or object to keep track of which keys are currently being pressed. When a keydown event is triggered, add the key to your tracking object. When a keyup event is triggered, remove the key from your tracking object. You can then check this object to see if the desired combination of keys is being pressed.

What is the difference between keypress, keydown, and keyup events?

The keydown event is triggered when a key is pressed down. The keypress event is similar, but it’s only triggered for keys that produce a character value, like letters and numbers. The keyup event is triggered when a key is released. In most cases, if you want to detect any key press, you should use the keydown event.

How can I simulate key press events programmatically?

You can simulate key press events programmatically by creating a new KeyboardEvent and dispatching it on an element. For example, to simulate pressing the ‘a’ key, you could do something like this:
var event = new KeyboardEvent('keydown', {key: 'a'});
document.body.dispatchEvent(event);
Remember that not all events can be simulated in all browsers due to security restrictions.

How can I prevent the default action of a key press in jQuery?

You can prevent the default action of a key press in jQuery by calling the event.preventDefault() method in your event handler. This will stop the browser from performing the default action associated with the key press.

How can I detect if a specific key is pressed in jQuery?

You can detect if a specific key is pressed in jQuery by checking the event.which property in your keydown or keyup event handler. This property will contain the key code of the pressed key. For example, to check if the ‘a’ key is pressed, you could do something like this:
$(document).keydown(function(event) {
if (event.which == 65) {
alert('You pressed the "a" key!');
}
});
Remember that key codes can vary between different browsers and operating systems, so it’s a good idea to use a library or a list of key codes to ensure compatibility.

How can I detect key press combinations like Ctrl+C or Ctrl+V in jQuery?

You can detect key press combinations like Ctrl+C or Ctrl+V in jQuery by checking the event.ctrlKey property in addition to the event.which property. The event.ctrlKey property will be true if the Ctrl key is being pressed. For example, to detect the Ctrl+C combination, you could do something like this:
$(document).keydown(function(event) {
if (event.ctrlKey && event.which == 67) {
alert('You pressed Ctrl+C!');
}
});
Remember that the key code for ‘C’ is 67 and for ‘V’ is 86.

How can I stop propagation of a key press event in jQuery?

You can stop propagation of a key press event in jQuery by calling the event.stopPropagation() method in your event handler. This will prevent the event from bubbling up the DOM tree and triggering handlers on parent elements.

How can I bind a function to a key press event in jQuery?

You can bind a function to a key press event in jQuery by using the keydown, keypress, or keyup methods. For example, to bind a function to the ‘a’ key press, you could do something like this:
$(document).keydown(function(event) {
if (event.which == 65) {
myFunction();
}
});
Where myFunction is the function you want to execute when the ‘a’ key is pressed.

How can I unbind a function from a key press event in jQuery?

You can unbind a function from a key press event in jQuery by using the off method. For example, to unbind the function bound to the ‘a’ key press in the previous example, you could do something like this:
$(document).off('keydown', myFunction);
Remember that you need to pass the same function reference to the off method that you passed to the keydown method.

How can I detect if multiple specific keys are pressed at the same time in jQuery?

You can detect if multiple specific keys are pressed at the same time in jQuery by using an array or object to keep track of the currently pressed keys, as explained in the answer to the first question. For example, to detect if the ‘a’ and ‘b’ keys are pressed at the same time, you could do something like this:
var keys = {};

$(document).keydown(function(event) {
keys[event.which] = true;

if (keys[65] && keys[66]) {
alert('You pressed "a" and "b" at the same time!');
}
});

$(document).keyup(function(event) {
delete keys[event.which];
});
Remember that the key code for ‘a’ is 65 and for ‘b’ is 66.

Sam DeeringSam Deering
View Author

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

jQuery
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week