C#捕获鼠标键盘未操作的时间长度

C#捕获鼠标键盘未操作的时间长度

代码
1 //添加引用
2  using System.Runtime.InteropServices;
3
4  //创建结构体用于返回捕获时间
5 [StructLayout(LayoutKind.Sequential)]
6 struct LASTINPUTINFO
7 {
8 //设置结构体块容量
9 [MarshalAs(UnmanagedType.U4)]
10 public int cbSize;
11
12 //捕获的时间
13 [MarshalAs(UnmanagedType.U4)]
14 public uint dwTime;
15 }
16
17 //引用系统API
18 [DllImport("user32.dll")]
19 private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
20 private static long GetLastInputTime()
21 {
22 LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
23 vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
24
25 //捕获时间
26 if (!GetLastInputInfo(ref vLastInputInfo)) return 0;
27 return Environment.TickCount - (long)vLastInputInfo.dwTime;
28 }
原文地址:https://www.cnblogs.com/yuxuan/p/1855586.html