码迷,mamicode.com
首页 > Windows程序 > 详细

C# 判断系统空闲(键盘、鼠标不操作一段时间)

时间:2014-12-19 14:21:28      阅读:338      评论:0      收藏:0      [点我收藏+]

标签:

利用windows API函数 GetLastInputInfo()来判断系统空闲

//添加引用 using System.Runtime.InteropServices;

 1   // 创建结构体用于返回捕获时间
 2   [StructLayout(LayoutKind.Sequential)]
 3   struct LASTINPUTINFO
 4   {
 5   // 设置结构体块容量
 6   [MarshalAs(UnmanagedType.U4)]
 7   public int cbSize;
 8   // 捕获的时间
 9   [MarshalAs(UnmanagedType.U4)]
10   public uint dwTime;
11   }
12   [DllImport("user32.dll")]
13   private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
14 // 获取键盘和鼠标没有操作的时间
15   private static long GetLastInputTime() 
16   {
17   LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
18   vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
19   // 捕获时间
20   if (!GetLastInputInfo(ref vLastInputInfo))
21        return 0;
22   else
23      return Environment.TickCount - (long)vLastInputInfo.dwTime;
24   } 

 

C# 判断系统空闲(键盘、鼠标不操作一段时间)

标签:

原文地址:http://www.cnblogs.com/SpeakHero/p/4173703.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!