1

我正在使用InactivityDetector代码来检测用户的不活动。但现在我面临一个问题,我需要在IdleTime不进行任何用户交互的情况下重置。

我已经尝试过用另一种方法来重现这个结果,比如模拟鼠标事件或按键,但它们都不起作用,因为它们不是来自用户的物理交互。

是的,我已经在StackOverflow中搜索了这么多解决方案,但没有找到解决我的问题的解决方案。任何人都可以在这方面提供帮助吗?

我的InactivityDetector班级:

public class InactivityDetector
    {
        [DllImport("user32.dll")]
        static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);


        public static IdleTimeInfo GetIdleTimeInfo()
        {
            int systemUptime = Environment.TickCount,
                lastInputTicks = 0,
                idleTicks = 0;

            LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
            lastInputInfo.cbSize = (uint)Marshal.SizeOf(lastInputInfo);
            lastInputInfo.dwTime = 0;

            foreach (Window openWin in Application.Current.Windows)
            {
                //ignore activity from pc when shell view is minimized (for counting unactivity only from application)
                if (openWin.DataContext != null && openWin.DataContext.GetType() == typeof(ShellViewModel) 
                    && openWin.WindowState != WindowState.Minimized)
                {
                    if (GetLastInputInfo(ref lastInputInfo))
                    {
                        lastInputTicks = (int)lastInputInfo.dwTime;

                        idleTicks = systemUptime - lastInputTicks;
                    }
                }
            }

            /*if (GetLastInputInfo(ref lastInputInfo))
            {
                lastInputTicks = (int)lastInputInfo.dwTime;

                idleTicks = systemUptime - lastInputTicks;
            }*/

            return new IdleTimeInfo
            {
                LastInputTime = DateTime.Now.AddMilliseconds(-1 * idleTicks),
                IdleTime = new TimeSpan(0, 0, 0, 0, idleTicks),
                SystemUptimeMilliseconds = systemUptime,
            };
        }
    }

    public class IdleTimeInfo
    {
        public DateTime LastInputTime { get; internal set; }

        public TimeSpan IdleTime { get; internal set; }

        public int SystemUptimeMilliseconds { get; internal set; }
    }

    internal struct LASTINPUTINFO
    {
        public uint cbSize;
        public uint dwTime;
    }
4

0 回答 0