[RESOLVED] Get notified when Mouse stops moving
I want to get notified when the mouse stops moving for a certain amount of time (say 0.5 sec). From Msdn documentaion, it looks like wathcing for WM_MOUSEHOVER messages is the way to go. I'm using Spy++ to check WM_MOUSEHOVER messages but I can see such message only on one window (eclipse window). I don't see any such message on any other window such as IE, firefox, notepad, windows explorer etc..I can see WM_MOUSELEAve message, but it deosn't help because I'm not interested in kowing when the mouse leaves a particular window. Is there any other way to track when the mouse movement comes to a halt ?
[627 byte] By [
kteegala] at [2007-11-20 11:38:29]

# 1 Re: [RESOLVED] Get notified when Mouse stops moving
Perhaps this way...
In a WM_MOUSEMOVE handler set a 'lastMoveTime' variable to the current time.
Setup a timer, and in the WM_TIMER handler, check if current time - lastMoveTime > your threshold.
# 2 Re: [RESOLVED] Get notified when Mouse stops moving
...I don't see any such message on any other window such as IE, firefox, notepad, windows explorer etc..You need to call TrackMouseEvent() with TME_HOVER in order to receive WM_NCMOUSEHOVER.
# 3 Re: [RESOLVED] Get notified when Mouse stops moving
Thanks
# 4 Re: [RESOLVED] Get notified when Mouse stops moving
Adding few cents, some detailed code:
1. First create a variable holding your minimum time (in milliseconds) after which you wish your window be notified, and a timer id:
//these are globals
UINT timer_id=153; // some unique integeral value > 0 for timer we will use
UINT report_after=3000; //report after 3 seconds
2. Within your window procedure, you track WM_MOUSEMOVE, if mouse is moving you remove timer if already set, save current system's tick count (that is in milliseconds), and start timer again.
static DWORD last_move;
if(message == WM_MOUSEMOVE) {
last_move=GetTickCount(); //save milliseconds in last_move
SetTimer(hwnd,timer_id,report_after,NULL);
}
3. Then you simply check the timer within window procedure, compare current ticks - last move with report_after which is 3000 (3 seconds), if condition met remove timer and take certain action:
if(message == WM_TIMER && (UINT)wParam==timer_id) {
if(GetTickCount()-last_move >= report_after) {
//well mouse stopped 3.x seconds ago
//it is notified so remove timer
KillTimer(hwnd,timer_id);
//Take desired action here
}
}
I am sure this helps you accomplish.
regards
# 5 Re: [RESOLVED] Get notified when Mouse stops moving
Adding few cents, some detailed code...I'd like to add some more.
- you do not have to kill a timer right before you set it - a simple call to SetTimer() will be enough.
- if you need to get notification in 3,000 ms, WHY do you set timer for 1 ms? First, it's NOT going to fire that often anyway, and second, you are loading your system for no reason at all. You could simply set that timer for 3,000 ms. That would relieve you from saving last move time and comparing it to the current time on every timer's tick.
# 6 Re: [RESOLVED] Get notified when Mouse stops moving
Good point about killing before setting again. I see, you mean when set timer again with same ID, will replace existing ?
and yes, great idea about setting timer to 3000 ms to save alot of processing.
Thanks for clearing up ValdimirF. I just have modified the code above. Please review it again, I think it is now exactly as you suggested.
regards
# 7 Re: [RESOLVED] Get notified when Mouse stops moving
Please review it again, I think it is now exactly as you suggested.Well, almost... This test is not need:
if(GetTickCount()-last_move >= report_after) (and almost always is true).
And that line is not needed also:
last_move=GetTickCount(); //save milliseconds in last_move
# 8 Re: [RESOLVED] Get notified when Mouse stops moving
Thanks for all the suggestions. I'm going to go ahead and try setting a timer in WM_MOUSEMOVe handler and take appropriate action in WM_TIMER handler.
So, I think my code would look something like this:
UINT timer_id=100;
UINT report_after=3000; (3 sec)
case WM_MOUSEMOVE:
SetTimer(hwnd,timer_id,report_after,NULL);
break;
case WM_TIMER:
// take appropriate action...say I'm printing the current cursor position
if (UINT)wParam==timer_id)
{
Point pt;
GetCursorPos(&pt);
printf("mouse stopped at x=%d; y=%d", pt.x, pt.y);
}
break;
I'm a little confused about not saving & comparing the last move time. Let's say mouse stops for 10 sec. Will the WM_TIMER be called every 3 sec and prints the cursor position 3 times? Or will it just print once (which is what I want)?
# 9 Re: [RESOLVED] Get notified when Mouse stops moving
Ok Never mind. I don't have to save the last move time but I've to kill the timer so that WM_TIMER handler is not called every 3 seconds after the mouse stops moving . So, if I didn't move the mouse for 10 sec, I would get 3 similar messages like:
mouse stopped at x=587 ;y=390
mouse stopped at x=587 ;y=390
mouse stopped at x=587 ;y=390
But I just want to be notified once after every halt. Now here is how my code looks. This way, once a timer is killed, the next timer is set only after the mouse moves.
UINT timer_id=100;
UINT report_after=3000; (3 sec)
case WM_MOUSEMOVE:
SetTimer(hwnd,timer_id,report_after,NULL);
break;
case WM_TIMER:
// take appropriate action...say I'm printing the current cursor position
if (UINT)wParam==timer_id)
{
Point pt;
GetCursorPos(&pt);
printf("mouse stopped at x=%d; y=%d", pt.x, pt.y);
}
KillTimer(hWnd,timer_id);
break;
Thanks for the help!
# 10 Re: [RESOLVED] Get notified when Mouse stops moving
last_move=GetTickCount(); //save milliseconds in last_move
Obviously it is no more needed, when timer is being reset on next mousemove.
# 11 Re: [RESOLVED] Get notified when Mouse stops moving
The OP has not asked it, but what about mouse is clicked, right-clicked or scrolled using? The mouse will NOT move, the above code works - but does it work in expected way?
Is it not required to handle "No Mouse Activity", and just "Mouse Move" only?
# 12 Re: [RESOLVED] Get notified when Mouse stops moving
Is it not required to handle "No Mouse Activity", and just "Mouse Move" only?
Good point. Yes, the above code wouldn't work if the idea is to wait for a delay in mouse activity. But I am just considering "No Mouse Movement" aspect because my objective is to get the mouse location once the mouse stops. So, the above code should work fine as long as the mouse location does not change (even when it's right clicked or scrolled). Thanks for the observation!!