c# - 为什么 Environment.TickCount 和键盘的 lastinputinfo.dwTime 是一样的?
问题描述
我正在努力获得使用鼠标和键盘的空闲时间。到目前为止,我得到了鼠标空闲时间,但我无法得到键盘的空闲时间。
当我检查“Environment.TickCount”和“lastInputInfo.dwTime”时,它们总是相同的,所以键盘上的空闲时间总是0。
这是获取最后输入时间的方法:
public DateTime GetLastInputTime()
{
var lastInputInfo = new LASTINPUTINFO();
lastInputInfo.dwTime = 0;
lastInputInfo.cbSize = (uint)Marshal.SizeOf(lastInputInfo);
GetLastInputInfo(ref lastInputInfo);
return DateTime.Now.AddMilliseconds(-(Environment.TickCount - lastInputInfo.dwTime));
}
这是获得空闲时间的方法:
public static uint GetIdleTime()
{
var lastinput = new LASTINPUTINFO();
lastinput.cbSize = (uint)Marshal.SizeOf(lastinput);
GetLastInputInfo(ref lastinput);
return ((uint)Environment.TickCount - lastinput.dwTime);
}
这是键盘事件:
private void keyboard_KeyBoardKeyPressed(object sender, EventArgs e)
{
if (GetIdleTime() > 10000)
{
timer1.Enabled = false;
MessageBox.Show("keyboard mvoe test");
timer1.Enabled = true;
}
label_keyboard.Text = FormatDateTime(lastInput.GetLastInputTime());
}
我设置了 10 秒来检查我是否在 10 秒内没有按键盘,消息会弹出。
解决方案
推荐阅读
- javascript - 带有 javascript promise .then() 的 IF 语句
- php - Cakephp 插件前缀不起作用
- python-3.x - AttributeError:无法腌制本地对象'Op.make_py_thunk。
运行 Pymc 代码时的 .rval - javascript - 如何在 Laravel 中调试 javascript(从浏览器控制台)?
- angularjs - 更改图表类型后,刻度线正在移动
- javascript - 无法在controllerFunction节点js中获取url参数
- android - android VideoView 无法播放此视频错误
- tensorflow - 最后的神经网络
- c# - 从资源中声明一个 const 字符串
- asp.net-core - 如何在 OpenIdConnect TokenValidated 偶数处理程序中访问 Session