Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 570
djdd87 Avatar asked Jun 24 '09 10:06

djdd87


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


2 Answers

GetLastInputInfo. Documented at PInvoke.net.

like image 183
Matthew Flaschen Avatar answered Sep 19 '22 19:09

Matthew Flaschen


include following namespaces

using System; using System.Runtime.InteropServices; 

and then include following

internal struct LASTINPUTINFO  {     public uint cbSize;      public uint dwTime; }  /// <summary> /// Helps to find the idle time, (in milliseconds) spent since the last user input /// </summary> public class IdleTimeFinder {     [DllImport("User32.dll")]     private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);              [DllImport("Kernel32.dll")]     private static extern uint GetLastError();      public static uint GetIdleTime()     {         LASTINPUTINFO lastInPut = new LASTINPUTINFO();         lastInPut.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);         GetLastInputInfo(ref lastInPut);          return ((uint)Environment.TickCount - lastInPut.dwTime);     } /// <summary> /// Get the Last input time in milliseconds /// </summary> /// <returns></returns>     public static long GetLastInputTime()     {         LASTINPUTINFO lastInPut = new LASTINPUTINFO();         lastInPut.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);         if (!GetLastInputInfo(ref lastInPut))         {             throw new Exception(GetLastError().ToString());         }                return lastInPut.dwTime;     } } 

To convert the tickcount into time you can use

TimeSpan timespent = TimeSpan.FromMilliseconds(ticks); 

Note. This routine uses the term TickCount but the values are in milliseconds and are so not the same as Ticks.

From MSDN article on Environment.TickCount

Gets the number of milliseconds elapsed since the system started.

like image 22
Sriwantha Attanayake Avatar answered Sep 21 '22 19:09

Sriwantha Attanayake