Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if the spacebar is being pressed and the mouse is moving at the same time with jQuery?

Tags:

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.

like image 324
Colin Avatar asked Feb 12 '10 02:02

Colin


People also ask

How to test the spacebar speed of the mouse?

Choose spacebar test in menu, Click "START" button or press the spacebar, Hit the space button as fast as you can, After time is up, you'll get your spacebar speed result.

How does the HTML detect when the space bar is clicked?

Whenever the space bar or the right arrow is clicked, the HTML will detect the type of click and respond by the number of times clicked or by a message. A number gets displayed when the space bar is once clicked.

How to track If the space bar is pressed or not?

56 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:

How do I run a spacebar test?

Go to cps-check.com, Choose spacebar test in menu, Click "START" button or press the spacebar, Hit the space button as fast as you can,


2 Answers

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;       console.log('space')     }   }); }); 

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

like image 124
cletus Avatar answered Oct 12 '22 13:10

cletus


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.

like image 39
John Boker Avatar answered Oct 12 '22 15:10

John Boker