views:

40

answers:

2

I want to have a event that only triggers when you press space and move your mouse at the same time. How can you make that?

+1  A: 

You could make one event that sets a variable to true when space is pressed down, and to false when it is released. Then you can check that variable in your mouseclick event.

Using closure, you can put this in your document ready function. This will work when clicking on the DOM with id "container"

(function () {
   var space = false;
   $("document").keyup(function(e) {
     if (e.keyCode == 32) {
       space = false;
     }
   }).keydown(function(e) {
     if (e.keyCode == 32) {
       space = true;
     }
   });
  $("#container").click(function() {
    if (space) {
      // Do action
    }
  });
})();
googletorp
It works, thanks! There is one problem. The page goes down when i press space. I tried to use e.preventDefault(); , but then it prevent whole document. I dont want it to prevent in for example input-tags
Codler