Skip to content

Instantly share code, notes, and snippets.

@piccaso
Created December 1, 2012 00:30
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save piccaso/4179759 to your computer and use it in GitHub Desktop.
Save piccaso/4179759 to your computer and use it in GitHub Desktop.
win32 GetLastInputInfo C# example
using System;
using System.Runtime.InteropServices;
using System.ComponentModel;
namespace GetLastUserInput
{
public class GetLastUserInput
{
private struct LASTINPUTINFO
{
public uint cbSize;
public uint dwTime;
}
private static LASTINPUTINFO lastInPutNfo;
static GetLastUserInput()
{
lastInPutNfo = new LASTINPUTINFO();
lastInPutNfo.cbSize = (uint)Marshal.SizeOf(lastInPutNfo);
}
[DllImport("User32.dll")]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
/// <summary>
/// Idle time in ticks
/// </summary>
/// <returns></returns>
public static uint GetIdleTickCount()
{
return ((uint)Environment.TickCount - GetLastInputTime());
}
/// <summary>
/// Last input time in ticks
/// </summary>
/// <returns></returns>
public static uint GetLastInputTime()
{
if (!GetLastInputInfo(ref lastInPutNfo))
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
return lastInPutNfo.dwTime;
}
}
}
@saber1in
Copy link

saber1in commented Oct 4, 2022

LASTINPUTINFO not found. Not connected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment