BrownChiLD BrownChiLD - 1 year ago 138
jQuery Question

How to detect control+click in Javascript from an onclick div attribute?

I need to know if the user is clicking or CONTROL CLICKING a div element..
i have seen examples on how to do it using event listeners.. but my codes are already set in place, and is using an on-element onclick method..


<div id='1' onclick='selectMe()'>blah</div>


function selectMe(){
//determine if this is a single click, or a cntrol click

...also would love to know if it was a left or right mouse button click.

Answer Source

I'd recommend using JQuery's keyup and keydown methods on the document, as it normalizes the event codes, to make one solution crossbrowser.

For the right click, you can use oncontextmenu, however beware it can be buggy in IE8. See a chart of compatibility here:

<p onclick="selectMe(1)" oncontextmenu="selectMe(2)">Click me</p>

        cntrlIsPressed = true;

    cntrlIsPressed = false;

var cntrlIsPressed = false;

function selectMe(mouseButton)
            case 1:
                alert("Cntrl +  left click");
            case 2:
                alert("Cntrl + right click");

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download