tags:

views:

349

answers:

5

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
A: 

Hope this helps

shahkalpesh
A: 
function onClick() {
    var e = window.event.srcElement;
    if (window.event.shiftKey) {
        ...
    }
}
jeffamaphone
-1: IE-specific.
eyelidlessness
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