我如何检查在点击事件期间用jquery是否按下键? [英] How can i check if key is pressed during click event with jquery?

查看:200
本文介绍了我如何检查在点击事件期间用jquery是否按下键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用jquery捕获一个点击事件,并能够告诉是否同时按下一个键,所以我可以在基于keypress事件的回调函数中fork。

I would like to catch a click event with jquery and be able to tell if a key was pressed at the same time so I can fork within the callback function based on the keypress event.

例如:

$("button").click(function() {
    if([KEYPRESSED WHILE CLICKED]) {
        // do something...
    } else {
        // do something different...
    }
});

有人知道这是否可能,或者如果可能,如何做? / p>

Does anyone know if this is possible at all or how it can be done if it is possible?

推荐答案

您可以轻松地从事件属性中检测shift,alt和控制键;

You can easily detect the shift, alt and control keys from the event properties;

$("button").click(function(evt) {
  if (evt.ctrlKey)
    alert('Ctrl down');
  if (evt.altKey)
    alert('Alt down');
  // ...
});

请参阅 quirksmode 了解更多属性。如果要检测其他键,请参阅 cletus的回答

See quirksmode for more properties. If you want to detect other keys, see cletus's answer.

这篇关于我如何检查在点击事件期间用jquery是否按下键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆