C# - Detect time of last user interaction with the OS

djdd87 picture djdd87 · Jun 24, 2009 · Viewed 33.8k times · Source

I'm writing a small tray application that needs to detect the last time a user interacted with their machine to determine if they're idle.

Is there any way to retrieve the time a user last moved their mouse, hit a key or interacted in any way with their machine?

I figure Windows obviously tracks this to determine when to display a screen saver or power down, etc, so I'm assuming there's a Windows API for retrieving this myself?

Answer

Matthew Flaschen picture Matthew Flaschen · Jun 24, 2009

GetLastInputInfo. Documented at PInvoke.net.