Capture a Shift and Click event with jQuery
JavascriptJqueryJavascript 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.