1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
public static class IdleTimeDetector
{
[DllImport("user32.dll")]
static extern bool GetLastInputInfo(out LastInputInfo plii);
[StructLayout(LayoutKind.Sequential)]
struct LastInputInfo
{
public static readonly int SizeOf = Marshal.SizeOf(typeof(LastInputInfo));
[MarshalAs(UnmanagedType.U4)]
public int cbSize;
[MarshalAs(UnmanagedType.U4)]
public int dwTime;
}
public static TimeSpan IdleSince
{
get
{
int idleTime = 0;
LastInputInfo lastInputInfo = new LastInputInfo();
lastInputInfo.cbSize = Marshal.SizeOf(lastInputInfo);
lastInputInfo.dwTime = 0;
if (GetLastInputInfo(out lastInputInfo))
{
idleTime = Environment.TickCount - lastInputInfo.dwTime;
}
return TimeSpan.FromMilliseconds(idleTime);
}
}
}
|