Capture a Shift and Click event with jQuery

JavascriptJquery

Javascript Problem Overview


I don't seem to be able to capture two events at the same time. I am trying to capture the Shift and Click (mouse click) event.

I have no problem capturing each action separately but not the two together.

Has anybody done some research on that particular problem?

Javascript Solutions


Solution 1 - Javascript

Yes:

$(document).click(function(e) {
    if (e.shiftKey) {
        alert("shift+click")
    } 
});

Solution 2 - Javascript

You can check the event.shiftKey boolean property.

$(selector).click(function(event) {
    if (event.shiftKey) {
        //....
    } 
});

Solution 3 - Javascript

Worth note:

to detect Ctrl or "Meta" (Cmd key on OS X)

$(document).click(function(e)
    {
        if (e.ctrlKey)
        {
            alert("ctrl+click");
        }
    });

$(document).click(function(e)
    {
        if (e.metaKey)
        {
            alert("CMD+click");
        }
    });

Solution 4 - Javascript

If I understand your question correctly, you can use the shiftKey property of the event object that you receive in your click handler to check whether the shift key was down when the user clicked.

EDIT: shiftKey, not shift

Solution 5 - Javascript

A nice library to handle key press events is shortcut.js, available here. It is very good.

You can even capture Crtl+Shift at the same time which is something I was looking for.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnawakView Question on Stackoverflow
Solution 1 - JavascriptChris Van OpstalView Answer on Stackoverflow
Solution 2 - JavascriptChristian C. SalvadóView Answer on Stackoverflow
Solution 3 - JavascriptDrewView Answer on Stackoverflow
Solution 4 - JavascriptSLaksView Answer on Stackoverflow
Solution 5 - JavascriptSMHMayboudiView Answer on Stackoverflow