Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
45
rated 0 times [  48] [ 3]  / answers: 1 / hits: 15833  / 15 Years ago, fri, july 31, 2009, 12:00:00

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


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


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

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.


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.


More From » jquery

 Answers
6

You can use the event.ctrlKey property.



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


Check a running example here.


[#99023] Monday, July 27, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jackie

Total Points: 442
Total Questions: 107
Total Answers: 94

Location: Honduras
Member since Sun, Dec 26, 2021
2 Years ago
jackie questions
Sat, Sep 18, 21, 00:00, 3 Years ago
Wed, Jul 14, 21, 00:00, 3 Years ago
;