[英]Javascript Detect Control Key Held on Mouseup
我搜索了很多,似乎找不到滿意的解決方案。 我希望有人能幫幫忙。
當我使用jQuery時,我也在編寫數千行Javascript。 所以一個“純粹的”JavaScript解決方案就好了。
我正在嘗試確定控制鍵是否在物理上按下鼠標事件。 而已; 沒有其他先決條件。 有沒有人知道如何可靠,跨瀏覽器這樣做?
我已經嘗試通過記錄按下和釋放按鍵時將其存儲在狀態變量中:
// 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);
}
任何幫助,將不勝感激。
您可以使用event.ctrlKey屬性。
$(function(){
$('#elementId').mouseup(function(e){
var isCtrlPressed = e.ctrlKey;
// true or false whether ctrl is pressed or not
});
});
在這里查看一個運行示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.