CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Sep 2006
    Posts
    38

    [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 ?

  2. #2
    Join Date
    May 2002
    Location
    Lindenhurst, NY
    Posts
    867

    Re: 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.

  3. #3
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Get notified when Mouse stops moving

    Quote Originally Posted by kteegala
    ...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.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  4. #4
    Join Date
    Sep 2006
    Posts
    38

    Re: Get notified when Mouse stops moving

    Thanks

  5. #5
    Join Date
    Nov 2004
    Location
    Pakistan
    Posts
    466

    Smile Re: 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:

    Code:
    //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.

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

    Code:
       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
    Last edited by Ali Imran; October 25th, 2007 at 11:24 PM.
    » Please 'Rate This Post' if it helped (encourage us to help you more)
    » Build GUI in minute using rad c++
    » Free IDE + GUI code generator - screenshot
    » Free WINAPI sourcecode and tutorials

  6. #6
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Get notified when Mouse stops moving

    Quote Originally Posted by Ali Imran
    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.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  7. #7
    Join Date
    Nov 2004
    Location
    Pakistan
    Posts
    466

    Re: 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
    » Please 'Rate This Post' if it helped (encourage us to help you more)
    » Build GUI in minute using rad c++
    » Free IDE + GUI code generator - screenshot
    » Free WINAPI sourcecode and tutorials

  8. #8
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: Get notified when Mouse stops moving

    Quote Originally Posted by Ali Imran
    Please review it again, I think it is now exactly as you suggested.
    Well, almost... This test is not need:
    Code:
    if(GetTickCount()-last_move >= report_after)
    (and almost always is true).
    And that line is not needed also:
    Code:
    last_move=GetTickCount();  //save milliseconds in last_move
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  9. #9
    Join Date
    Sep 2006
    Posts
    38

    Re: 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)?
    Last edited by kteegala; October 26th, 2007 at 01:16 PM.

  10. #10
    Join Date
    Sep 2006
    Posts
    38

    Re: 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!
    Last edited by kteegala; October 26th, 2007 at 01:15 PM.

  11. #11
    Join Date
    Nov 2004
    Location
    Pakistan
    Posts
    466

    Re: 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.
    » Please 'Rate This Post' if it helped (encourage us to help you more)
    » Build GUI in minute using rad c++
    » Free IDE + GUI code generator - screenshot
    » Free WINAPI sourcecode and tutorials

  12. #12
    Join Date
    Mar 2003
    Location
    India {Mumbai};
    Posts
    3,871

    Re: 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?
    My latest article: Explicating the new C++ standard (C++0x)

    Do rate the posts you find useful.

  13. #13
    Join Date
    Sep 2006
    Posts
    38

    Re: Get notified when Mouse stops moving

    Quote Originally Posted by Ajay Vijay
    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!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured