GetLastInputInfoHelp.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace Muchinfo.MTPClient.Service
  4. {
  5. public class GetLastInputInfoHelp
  6. {
  7. [DllImport("user32.dll")]
  8. internal static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
  9. /// <summary>
  10. /// 获取鼠标键盘不活动的时间
  11. /// </summary>
  12. /// <returns>结果</returns>
  13. public static int GetLastInputTime()
  14. {
  15. LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
  16. lastInputInfo.cbSize = Marshal.SizeOf(lastInputInfo);
  17. lastInputInfo.dwTime = 0;
  18. int idleTime = 0;
  19. if (GetLastInputInfo(ref lastInputInfo))
  20. {
  21. idleTime = Environment.TickCount - lastInputInfo.dwTime;
  22. }
  23. return ((idleTime > 0) ? (idleTime / 1000) : 0);
  24. }
  25. }
  26. [StructLayout(LayoutKind.Sequential)]
  27. internal struct LASTINPUTINFO
  28. {
  29. [MarshalAs(UnmanagedType.U4)]
  30. public int cbSize;
  31. [MarshalAs(UnmanagedType.U4)]
  32. public int dwTime;
  33. }
  34. }