Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shift + mouseover with jQuery

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.

like image 667
Colin Brock Avatar asked Sep 17 '09 20:09

Colin Brock


1 Answers

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

   }
like image 195
marcgg Avatar answered Nov 15 '22 21:11

marcgg