How can I write an onclick handler that does one thing for regular clicks and a different thing for shift-clicks?
+9
A:
You can look at the click event's shiftKey property.
window.addEventListener("click",
function(e){
if(e.shiftKey) alert("Shift, yay!");
},
false);
AKX
2009-04-06 21:26:07
A:
function onClick() {
var e = window.event.srcElement;
if (window.event.shiftKey) {
...
}
}
jeffamaphone
2009-04-06 21:27:01
-1: IE-specific.
eyelidlessness
2009-04-07 07:31:32
A:
The event fired from the DOM ought to contain a shiftKey
(or equivalent) property indicating the state of the shift key when the event was fired; see, e.g. https://developer.mozilla.org/en/DOM/event.shiftKey for an example.
If you're using a JavaScript/DOM wrapping library such as YUI, Prototype or jQuery, any differences in implementation ought not to be an issue.
Rob
2009-04-06 21:29:18