PDA

View Full Version : function - element is clicked while SHIFT key is pressed


dhruvg
03-25-2009, 08:48 AM
hi.

i am trying to build a function that takes into account a click event AND SHIFT key press event on a particular element.

so for example, i want to do something only when i click a box WHILE pressing the SHIFT key.

how do i program this using jquery?

thanks much!

Fumigator
03-25-2009, 04:54 PM
Interesting... typically shift+click isn't an event that is captured by Javascript, because it's not low-level enough (such as C). But.. there is the keydown event, so in theory you can detect when the shift key is pressed down (see the coding example on this page (http://docs.jquery.com/Events/keydown)) and when a key is released (keyup), so I don't see why you couldn't turn a boolean variable on when shift is pressed down and back off when shift is released, and then you can check inside your click event function to see if that variable is on or off (which would indicate if shift is pressed or not). I'll have to try that out; it could come in handy.

TinyScript
03-25-2009, 05:03 PM
Here's an event capture, but shift is not one of the events triggered. I'd like to know how to do it too. I'm going to hit the google.



<html>
<head>
</head>
<body>


<script type="text/javascript">
function handler(e) {
var eve = (e?e:event);
var code = (eve.charCode?eve.charCode:eve.keyCode);
document.body.appendChild(document.createTextNode("You press :"+code));
document.body.appendChild(document.createElement("br"));
alert(eve);
}
document.onkeypress=handler;
document.body.appendChild(document.createTextNode(f.xd.onkeypress));
</script>
</body>
</html>

Fumigator
03-25-2009, 05:11 PM
...And as I was hoping, a plugin has been written for this very thing, called extendedclick. It will detect shift+click, alt+click, ctrl+click and combinations of shift, alt, and ctrl. HANDY!

http://plugins.jquery.com/project/extendedclick

Demonstration:

http://minuscreative.com/jquery