We help IT Professionals succeed at work.

Mouse movement across any control

ssdjgru1
ssdjgru1 asked
on
Medium Priority
293 Views
Last Modified: 2010-04-16
I am trying to make an application that logs out if the user is inactive  (mouse hadn't moved) for certain time. Therefore I need to catch any mouse movement that occours over my program. The problem is that I would not like to make "onMouseMove" event for each and every control to reset the Time Out Timer. Is there a way to monitor the mouse for whole focused application?
(PS: Sorry for stingy points, but I put 500 points on another question, that I resolved myself and now waiting for refound - I will raise the award the moment I get back my points!)
Comment
Watch Question

Senior Software Engineer
Commented:
Do you want to find out idle time of user? There is an event to that.

But first to your question. Yes, there is way to monitor mouse. One way is to call (in timer event for example) Control.MousePosition and see if it has moved since last call:

private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
      Point oldPos = new Point();
      Point newPos = Control.MousePosition;
      if (newPos.X == oldPos.X && newPos.Y == oldPos.Y)
      {
            // Mouse hasn't moved
      }
}

Of course mouse could be stashed away while user is in chat window or writing a mail. Therefore I suggest checking against idle time. You can bind Application.Idle EventHandler or idle time tracker.

Good examples can be found within this forum:
Application.Idle event handling (http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20918405.html?query=idle&topics=327)

In Application class you have static event Idle, you can use it following way:
Form1_Load(..)
{
...
  Application.Idle += new EventHandler(this.OnIdle);
...
}
private void OnIdle(object sender, System.EventArgs args)
{
//Do anything you want, IDLE-TIME!!!
}

Or idle time tracking (http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_20933068.html?query=idle&topics=327).

I hope this helps (and 32 points is fine for easy question like this).

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thank you for your anwser!

If you are satisfied with 32 points, I am accepting your anwser right now ;)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.