using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace Dll { public class MouseKeys { /// ///获取鼠标闲置时间 /// [StructLayout(LayoutKind.Sequential)] public struct LASTINPUTINFO { [MarshalAs(UnmanagedType.U4)] public int cbSize; [MarshalAs(UnmanagedType.U4)] public uint dwTime; } /// ///获取鼠标闲置时间 /// [DllImport("user32.dll")] public static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); /// ///获取闲置时间 /// public static long GetIdleTickS() { LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO(); vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo); if (!GetLastInputInfo(ref vLastInputInfo)) return 0; return Convert.ToInt16((Environment.TickCount - (long)vLastInputInfo.dwTime) / 1000); } } }