Google Maps API and "rightclick" events on Macs

Posted by samc on Stack Overflow See other posts from Stack Overflow or by samc
Published on 2010-06-02T05:18:15Z Indexed on 2010/06/02 5:23 UTC
Read the original article Hit count: 241

Using the Google Maps API (v3), I can create a map and handle normal click events just fine, but when I want to handle rightclick events, it doesn't work on Macs. I assume this is because a rightclick on a Mac is actually converted to a ctrl-click, but the Google Maps API MouseEvent doesn't provide information about modifier keys, so I can't check for the ctrl key.

I tried adding an "capture" event listener to the document that converts the click event to a rightclick event.


function convertClick(e) {
    if (e.ctrlKey) {
        e.button = 2;
    }
}
document.addEventListener("click", convertClick, true)

I added an alert to verify that the condition is correct, but modifying the event in this way didn't work.

So, I decided to have my event handler set a global flag that my click handler could check. If the flag is set, it means ctrl was pressed, so the click handler just invokes the rightclick handler.


var ctrl;
function captureCtrl(e) {
    ctrl = e.ctrlKey;
}

This approach worked great, except for one thing. The ctrl flag gets set for the click after the one that occured when ctrl was pressed. That means the event handler is be called during the bubble phase rather than the capture phase. Could explain why the event modification approach didn't work.

So, my question is how can you detect "rightclick" events from Macs with the Google Maps API? I can't be the first person to want to do this. That said, when I right-click on the map on http://maps.google.com from a Windows or Linux machine, I get a popup box with options like "Directions from here...", etc. On a Mac, nothing happens. So, not even the main Google Maps page has solved this problem. ...maybe I am the first person to want to do this.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about mac