Ho do I capture ctrl + click on MacOS with d3js

I got a report that my script which should trigger on ctrl + click does not work on Mac.

Like shown in "Determine if Shift key is pressed during mousedown event" we can determine if modifier keys are pressed during a mouse click testing d3.event.shiftKey, d3.event.ctrlKey etc. The shift detection works everywhere but ctrl does not seem to work on MacOS.

<div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"> <div class="snippet-code">

d3.select(window).on("click", function() {
    if (d3.event.ctrlKey) {
        alert("Mouse + Ctrl pressed");
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>


I don't have a Mac but I tested the script above using BrowserStack with:

    <li>Mavericks with Safari 7.1, Firefox 54 and Chrome 60;</li> <li>Sierra with Safari 10.1, Firefox 54 and Chrome 60</li> </ul>

    It is not working with any of the browsers on Mac. It works fine on Windows and Linux though.

    What do I do wrong? Is the ctrl key some kind of a special key on Mac OSes (I suppose it is as Mac has also the 'command' key). Is using the ctrl + click discouraged for Mac OS compatibility?

    Edit: I found this one: "any way to detect ctrl + click in javascript for osx browsers? no jQuery". My questions still holds as using d3.js framework I would expect that there is a way to do this in a cross-browser compatible way using d3.event.

    Answer1:

    You can check this in the following way:

    if (d3.event.ctrlKey || d3.event.metaKey) { alert("Mouse + Ctrl pressed (or command key for Mac users)"); }

    人吐槽 人点赞

Recommend

Comment

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:Ho do I capture ctrl + click on MacOS with d3js