问题
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