问题描述
我搜索了很多,似乎找不到满意的解决方案。我希望有人可以提供帮助。
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.
推荐答案
您可以使用属性。
$(function(){
$('#elementId').mouseup(function(e){
var isCtrlPressed = e.ctrlKey;
// true or false whether ctrl is pressed or not
});
});
查看正在运行的示例。
这篇关于Javascript检测在Mouseup上保持的控制键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!