• C#获取键盘和鼠标操作的时间的类


    最近在开发项目时需要实现屏保功能,即用户在设定的时间内没有对系统进行操作时,系统将会自动进入屏保状态。

    为此封装了一个获取鼠标键盘动作的类,并有一个方法可以返回用户多长时间没有操作系统的时间。

    代码如下:

    public class MouseKeyBoardOperate
        {
            /// <summary>
            /// 创建结构体用于返回捕获时间
            /// </summary>
            [StructLayout(LayoutKind.Sequential)]
            struct LASTINPUTINFO
            {
                /// <summary>
                /// 设置结构体块容量
                /// </summary>
                [MarshalAs(UnmanagedType.U4)]
                public int cbSize;

                /// <summary>
                /// 抓获的时间
                /// </summary>
                [MarshalAs(UnmanagedType.U4)]
                public uint dwTime;
            }

            [DllImport("user32.dll")]
            private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
            /// <summary>
            /// 获取键盘和鼠标没有操作的时间
            /// </summary>
            /// <returns>用户上次使用系统到现在的时间间隔,单位为秒</returns>
            public static long GetLastInputTime()
            {
                LASTINPUTINFO vLastInputInfo = new LASTINPUTINFO();
                vLastInputInfo.cbSize = Marshal.SizeOf(vLastInputInfo);
                if (!GetLastInputInfo(ref vLastInputInfo))
                {
                    return 0;
                }
                else
                {
                    long count = Environment.TickCount - (long)vLastInputInfo.dwTime;
                    long icount = count / 1000;
                    return icount;
                }
            }

        }

  • 相关阅读:
    EntityFramework优缺点
    领导者与管理者的区别
    七个对我最好的职业建议(精简版)
    The best career advice I’ve received
    Difference between Stored Procedure and Function in SQL Server
    2015年上半年一次通过 信息系统项目管理师
    Difference between WCF and Web API and WCF REST and Web Service
    What’s the difference between data mining and data warehousing?
    What is the difference between a Clustered and Non Clustered Index?
    用new创建函数的过程发生了什么
  • 原文地址:https://www.cnblogs.com/kevinGao/p/2181464.html
Copyright © 2020-2023  润新知