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>我没有Mac,但是我使用BrowserStack通过以下测试了上面的脚本:具有Safari 7.1,Firefox 54和Chrome 60的小牛;具有Safari 10.1,Firefox 54和Chrome 60的Sierra它不适用于Mac上的任何浏览器.不过,它在Windows和Linux上都能正常工作.我做错了什么? ctrl键是Mac OS上某种特殊的键(我想是因为Mac也有'command'键).不建议使用ctrl + click以获得Mac OS兼容性?我发现了这个:"检测ctrl的任何方法+在osx浏览器中单击javascript都可以吗?不需要jQuery ".我的问题仍然存在,因为我使用d3.js框架,我希望有一种方法可以使用d3.event以跨浏览器兼容的方式来做到这一点.解决方案您可以通过以下方式进行检查:if (d3.event.ctrlKey || d3.event.metaKey) { alert("Mouse + Ctrl pressed (or command key for Mac users)");}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 60It 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. 解决方案 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)");} 这篇关于我如何捕获Ctrl +单击带有d3js的MacOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 17:21
查看更多