I'm trying to detect whether the shift key is being pressed while the cursor is moved over a particular element. The function fires, but only after I click on another element first. Is there some way to work around this? I've tried setting focus to both the document and element, and tried creating a pseudo-click function but so far nothing has worked.
For example, the following code works only after I click another element on the page:
$("#selector").mouseover(function(e){
if(e.shiftKey) {
console.log("the shift key is pressed");
}
});
Thanks in advance for any information.
check this on the keypress event:
$(document).keypress(function (e) {
if(e.shiftKey) {
pressed = true; // pressed is a global varialbe. Be carefull of the scope
}
}
then on the keyup:
$(document).keyup(function(event){
pressed = false;
});
then do:
$("#selector").mouseover(function(e){
if(pressed) {
console.log("the shift key is pressed");
}
});
or the other way around :
$("#selector").mouseover(function(e){
isover = true;
});
and
$(document).keypress(function (e) {
if(e.shiftKey) {
alert("do something")
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With