19

我已经搜索了很多,似乎无法找到令人满意的解决方案。我希望有人能帮帮忙。

在我使用 jQuery 的同时,我也在编写数千行 Javascript。所以一个“纯”的javascript解决方案就可以了。

我正在尝试确定控制键是否在 mouseup 事件上被物理按住。就是这样; 没有其他先决条件。有谁知道如何可靠地跨浏览器做到这一点?

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

// 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 进行测试并观察控制台,您会看到似乎发生了自动重复,并且值切换。

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

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);
}

任何帮助,将不胜感激。

4

1 回答 1

35

您可以使用event.ctrlKey属性。

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

在此处查看运行示例。

于 2009-07-31T03:49:35.027 回答