14

I'd like to pause my program if a user is inactive for 5 minutes. By inactive I mean hasn't pressed their mouse or their keyboard during that time (including outside the program too!). Any starting points?

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
Andrew
  • 2,519
  • 6
  • 29
  • 46

5 Answers5

11

Within a timer you could p/invoke GetLastInputInfo() which will return the number ms since input was detected from the user, across all processes in the current session.

Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • Seems like what I need, but the weird thing is that LASTINPUTINFO only increases when I'm moving my mouse. – Andrew Jun 08 '11 at 18:51
  • 4
    It returns the tick-time of the last input so it will go up, deduct it from Environment.TickCount – Alex K. Jun 08 '11 at 18:57
6

This question is very similar to an old question:

.NET equivalent for GetLastInputInfo?

Which referenced a good article describing some different options in C#:

http://www.codeproject.com/KB/cs/uim.aspx

If you don't mind using P/Invoke and being limited to running on windows systems, then using P/Invoke to call GetLastInputInfo() is probably the simplest method of accomplishing what you want.

Community
  • 1
  • 1
dschaeffer
  • 618
  • 6
  • 15
2

you could override the WndProc and look for the WM_IDLE message

Matthew Sanford
  • 1,069
  • 1
  • 13
  • 21
2

I've used the GMA.UserActivityMonitor library a while ago to achieve this.

DanielB
  • 19,910
  • 2
  • 44
  • 50
0

Looks like I found solution for Your problem, look at:

How to detect a Winforms app has been idle for certain amount of time and user34660 answer,

For your app, must change:

const int MinuteMicroseconds = 60000;

to

const int MinuteMicroseconds = 300000;

It's just time, but in ms.

Works nice for my Windows Forms application.

Antoine Thiry
  • 2,362
  • 4
  • 28
  • 42
only_one
  • 492
  • 5
  • 16