C# - 检测上次用户与操作系统交互的时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1037595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# - Detect time of last user interaction with the OS
提问by djdd87
I'm writing a small tray application that needs to detect the last time a user interacted with their machine to determine if they're idle.
我正在编写一个小托盘应用程序,它需要检测用户最后一次与其机器交互的时间,以确定他们是否空闲。
Is there any way to retrieve the time a user last moved their mouse, hit a key or interacted in any way with their machine?
有什么方法可以检索用户上次移动鼠标、击键或以任何方式与机器交互的时间?
I figure Windows obviously tracks this to determine when to display a screen saver or power down, etc, so I'm assuming there's a Windows API for retrieving this myself?
我认为 Windows 显然会跟踪它以确定何时显示屏幕保护程序或关闭电源等,所以我假设有一个 Windows API 可以自己检索它?
采纳答案by Matthew Flaschen
GetLastInputInfo. Documented at PInvoke.net.
回答by Sriwantha Attanayake
include following namespaces
包括以下命名空间
using System;
using System.Runtime.InteropServices;
and then include following
然后包括以下内容
internal struct LASTINPUTINFO
{
public uint cbSize;
public uint dwTime;
}
/// <summary>
/// Helps to find the idle time, (in milliseconds) spent since the last user input
/// </summary>
public class IdleTimeFinder
{
[DllImport("User32.dll")]
private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
[DllImport("Kernel32.dll")]
private static extern uint GetLastError();
public static uint GetIdleTime()
{
LASTINPUTINFO lastInPut = new LASTINPUTINFO();
lastInPut.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);
GetLastInputInfo(ref lastInPut);
return ((uint)Environment.TickCount - lastInPut.dwTime);
}
/// <summary>
/// Get the Last input time in milliseconds
/// </summary>
/// <returns></returns>
public static long GetLastInputTime()
{
LASTINPUTINFO lastInPut = new LASTINPUTINFO();
lastInPut.cbSize = (uint)System.Runtime.InteropServices.Marshal.SizeOf(lastInPut);
if (!GetLastInputInfo(ref lastInPut))
{
throw new Exception(GetLastError().ToString());
}
return lastInPut.dwTime;
}
}
To convert the tickcount into time you can use
要将滴答计数转换为时间,您可以使用
TimeSpan timespent = TimeSpan.FromMilliseconds(ticks);
Note. This routine uses the term TickCount but the values are in milliseconds and are so not the same as Ticks.
笔记。此例程使用术语 TickCount,但值以毫秒为单位,因此与 Ticks 不同。
From MSDN article on Environment.TickCount
来自关于 Environment.TickCount 的 MSDN 文章
Gets the number of milliseconds elapsed since the system started.
获取自系统启动以来经过的毫秒数。
回答by Luciano Rodrigues Nunes Mendes
Code:
代码:
using System;
using System.Runtime.InteropServices;
public static int IdleTime() //In seconds
{
LASTINPUTINFO lastinputinfo = new LASTINPUTINFO();
lastinputinfo.cbSize = Marshal.SizeOf(lastinputinfo);
GetLastInputInfo(ref lastinputinfo);
return (((Environment.TickCount & int.MaxValue) - (lastinputinfo.dwTime & int.MaxValue)) & int.MaxValue) / 1000;
}