any way to detect ctrl + click in javascript for osx browsers? no jQuery

与世无争的帅哥 提交于 2019-11-28 06:27:24

问题


using vanilla js. Any way to grab the "right-click" (option-click) from OSX?

function clickey(e)
{
  if(event.button==2 || /*how you'd do it in Java=)*/ e.getButton() == MouseButton.BUTTON3 )    
...    
}

but in js, how do eeet?


回答1:


You need to listen to the contextmenu event. This is triggered when the context menu should be shown. So either if the right mouse butten or or ctrl + mouse.

If it is not supported then you can try to check the mousedown event where button is 2 and ctrlKey is true if it is triggered by using ctrl + mouse

document.addEventListener("contextmenu",function(event){

});

OR (depending on what the browser supports)

document.addEventListener("mousedown",function(event){
    if( event.ctrlKey || event.button == 2 ) {
    }
});

edit: removed the which info




回答2:


I'm not experienced with OSX, but the Mouse Events have the option to check the modifier keys. So something along these lines should work:

DOMElement.addEventListener("click",function(event){
   // either check directly the button
   if (event.button == 2){}
   // or
   if (event.ctrlKey || event.altKey || event.metaKey){
       // do stuff
   }  
});


来源:https://stackoverflow.com/questions/14725824/any-way-to-detect-ctrl-click-in-javascript-for-osx-browsers-no-jquery

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