incluyen siguientes espacios de nombres
using System;
using System.Runtime.InteropServices;
y luego incluir siguiente
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;
}
}
Convertir el TickCount en el tiempo se puede utilizar
TimeSpan timespent = TimeSpan.FromMilliseconds(ticks);
Nota. Esta rutina usa el término TickCount, pero los valores están en milisegundos y no son lo mismo que Ticks.
De MSDN article on Environment.TickCount
Obtiene el número de milisegundos transcurridos desde que se inició el sistema.
perfecto, gracias. – GenericTypeTea
+1 para PInvoke.net - No sabía acerca de ese recurso hasta ahora. –
no funciona para pantallas táctiles sin actividad de mouse y teclado – Smith