Javascript检测在Mouseup上保持的控制键 [英] Javascript Detect Control Key Held on Mouseup

查看:91
本文介绍了Javascript检测在Mouseup上保持的控制键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了很多,似乎找不到满意的解决方案。我希望有人可以提供帮助。

I've searched a good deal, and can't seem to find a satisfactory solution. I hope someone can help.

当我使用jQuery时,我也在编写数千行Javascript。所以一个纯粹的JavaScript解决方案就好了。

While I am using jQuery, I am also writing many thousands of lines of Javascript. So a "pure" javascript solution is just fine.

我正在尝试确定控制键是否在物理上压低了mouseup事件。而已;没有其他先决条件。有没有人知道如何可靠,跨浏览器这样做?

I'm trying to determine if the control key is physically held down on a mouseup event. That's it; there are no other preconditions. Does anyone know how to do this reliably, cross-browser?

我已经尝试通过注意按下和释放按键时将其存储在状态变量中:

I've tried storing this in a state variable by noting when the key is pressed and released:

// BEGIN store control key status in hash_state
$().bind('keydown','ctrl',function( arg_obj_e ){
  hash_state.sw_ctrldn = true;
  console.debug( hash_state.sw_ctrldn );
});
$().bind('keyup','ctrl',function( arg_obj_e ){
  hash_state.sw_ctrldn = false;
  console.debug( hash_state.sw_ctrldn );
});
// END store control key status in hash_state

然而,这确实不起作用。如果你使用firebug测试它并观察控制台,你会发现自动重复似乎发生了,并且值切换。

However, this really doesn't work. If you test this using firebug and watch the console, you will see that auto-repeat seems to happen, and the value toggles.

我检查了mouseup事件以查看是否那里有一些有用的东西,但无济于事:

I inspected the mouseup event to see if there is anything useful there, but to no avail:

var debugEvent = function( arg_obj_e ){
  var str = '';
  for ( var attr in arg_obj_e ){
    str += attr + ': ' + arg_obj_e[attr] + '\n';
  }
  console.debug(str);
}

任何帮助都将不胜感激。

Any help would be appreciated.

推荐答案

您可以使用活动。 ctrlKey 属性。

$(function(){
  $('#elementId').mouseup(function(e){
    var isCtrlPressed = e.ctrlKey;
    // true or false whether ctrl is pressed or not 
  });
});

查看正在运行的示例这里

这篇关于Javascript检测在Mouseup上保持的控制键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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