Link to home
Start Free TrialLog in
Avatar of urif
urif

asked on

detect a mouse moving

i currently have a small application that performs background tasks every time the user moves the mouse N pixels. i am using SetWindowsHookEx together with CallNextHookEx to create the system wide mouse hook. it works just fine.
the thing is that the message returns these values:

WM_LBUTTONUP
WM_LBUTTONDOWN
WM_RBUTTONDOWN
WM_RBUTTONUP
WM_MOUSEWHEEL
WM_MOUSEMOVE

but not "mouse moving"

the problem i have at the moment is that i shouldn't perform those tasks while the mouse is moving, i should wait until the mouse stoped and then perform the task.
i tried using the system idle time but that also applies to the keyboard which is no good.

is there a way to detect (system wide) when the mouse is moving and when it's not?
sort of like:

if IsMouseMoving then
   do it
else
  don't

IsMouseMoving should return 0 for no and 1 for yes or whatever.

thanks in advance
Avatar of RadikalQ3
RadikalQ3
Flag of Spain image

Hi!

When you receive the WM_MouseMove, can turn on a timer, and, if the timer reach the countdown, the mouse is quiet, else... is moving.





Avatar of urif
urif

ASKER

thanks for the answer.

but i don't get it, call me dumb. what you are saying is thati have a timer set to enable=false. if the mouse is moved  (WM_MouseMove) then i set it to true and then?
what if the mouse keeps on moving, it'll eventually timeout the timer.
can you give me an example please?

thanks again
The question is:  In your opinion what is a stopped mouse?
If I move the mouse... hummm 1 pixel per second... its stopped?
There is a time for considering the mouse sttopped...
If you have a global mouse hook, you are receiving a message in each mouse movement so... you must measure how many time is you hook without receive any movement message for gime the 'stop' state...

I havent now a mouse hook, but... let me suppose that I simulate the MouseMove received message pressing a Button and executing the procedure called: MouseMoveReceived

In this procedure (the button press in my case and the WM_Mousemove receiver in your hook case) I have:

procedure TForm1.MouseMoveReceived;
begin
  //Restart the countdown...
  Timer1.Enabled:=FALSE;
  Timer1.Enabled:=TRUE;
  Stopped:=FALSE;
end;


And in the timer I have:

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  ShowMessage('The Mouse is stopped');
  Stopped:=TRUE;
  Timer1.Enabled:=FALSE;
end;

The timer is disabled from Delphi Ide.
Now if you run the application... if you press the button several times, the timer doesnt reach the countdown so... the mouse is in movement, but, if you dont press the button (in your case dont receive mouse movement messages) the timer reach the countdown and signal the Stoped state of the mouse...

Simply call to the procedure MouseMoveReceived each time than your mouse hook receive an WM_MouseMove message...

ASKER CERTIFIED SOLUTION
Avatar of RadikalQ3
RadikalQ3
Flag of Spain image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of urif

ASKER

i had to add the checking of position (X and Y) and see if it was different, but other than that it's great