Ho do I capture ctrl + click on MacOS with d3js

只谈情不闲聊 提交于 2020-01-24 09:12:05

问题


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.

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:

  • Mavericks with Safari 7.1, Firefox 54 and Chrome 60;
  • Sierra with Safari 10.1, Firefox 54 and Chrome 60

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.


回答1:


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)");
}


来源:https://stackoverflow.com/questions/45438932/ho-do-i-capture-ctrl-click-on-macos-with-d3js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!