views:

278

answers:

2

Is there a way to check if the space bar and at the same time track what direction the mouse is moving and how far etc.

Point of this is that I want to replicate how Photoshop scrolls when you hold the space bar, left mouse button and you move the mouse, but without having to hold down the left mouse button.

+8  A: 

You can use keydown() and keyup() to track if the space bar is pressed or not and look at that state in your mousemove() event handler. For example:

var space = false;
$(function() {
  $(document).keyup(function(evt) {
    if (evt.keyCode == 32) {
      space = false;
    }
  }).keydown(function(evt) {
    if (evt.keyCode == 32) {
      space = true;
    }
  });
}

And then your mousemove() handler can see if it's pressed or not.

cletus
+4  A: 

you will probably have to be watching for the keydown event, check to see that it's the spacebar, set a variable saying it's down, unset it when the keyup event is seen.

so, then you would look for mouse movements when that variable was set indicating the spacebar was pressed.

John Boker
+1 Beat me to it.
Jimmy Cuadra