简体   繁体   English

用于“Ctrl”/“Shift”+鼠标左键单击的JavaScript或jQuery事件处理程序

[英]JavaScript or jQuery event handlers for “Ctrl”/“Shift” + mouse left button click

Is it possible to handle such events as: 是否可以处理以下事件:

  • Ctrl + mouse left button click; 按Ctrl +鼠标左键单击;
  • Shift + mouse left button click; Shift +鼠标左键单击;
  • Alt + mouse left button click by using JavaScript , jQuery or other framework. 使用JavaScriptjQuery或其他框架单击Alt +鼠标左键单击。

If it is possible, please give a code example for it. 如果可能,请给出一个代码示例。

You can do something like this (jQuery for the click handler, but any framework works on the part that matters): 你可以做这样的事情(点击处理程序的jQuery,但任何框架都适用于重要的部分):

$(selector).click(function(e) {
  if(e.shiftKey) {
    //Shift-Click
  }
  if(e.ctrlKey) {
    //Ctrl+Click
  }
  if(e.altKey) {
    //Alt+Click
  }
});

Just handle whichever you want inside an if inside the click handler like I have above. 只要处理无论你想内的if点击处理程序中像我上面。

如果你使用名为热键的 JQuery插件,你可以处理下面的特殊键。

$(document).bind('keydown', 'Ctrl+c', fn);

More recently I encountered a problem with using e.ctrlKey in that, it does not work on MACs. 最近我遇到了使用e.ctrlKey的问题,它在MAC上不起作用。 In a Macintosh, the same effect is achieved using Command+Click. 在Macintosh中,使用Command + Click可以实现相同的效果。

Since most of the answers above are already assuming usage of jQuery, you can simply use the e.metaKey property which is made available by jQuery. 由于上面的大多数答案都已经假设使用了jQuery,因此您可以简单地使用jQuery提供的e.metaKey属性。

eg 例如

$(selector).click(function(e) {
  if(e.shiftKey) {
    //Shift-Click
  }
  if(e.metaKey) {
    //Ctrl+Click on Windows & Command+Click on Mac.
  }
  if(e.altKey) {
    //Alt+Click
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM