我正在编写一个相对简单的 C# 项目。想想“公共网络终端”。本质上,有一个最大化的表单,上面有一个填充停靠的 Web 浏览器。我使用的 Web 浏览器控件是此处的 WebKit 控件:
我试图通过保持表示最后一次鼠标移动或按键操作的 DateTime 来检测系统空闲时间。
我已经建立了事件处理程序(参见下面的代码),并且遇到了一个绊脚石。当我的鼠标在 Web 文档上移动时,鼠标(和键)事件似乎没有触发。当我的鼠标触摸 Web 浏览器控件的垂直滚动条部分时,它确实可以正常工作,所以我知道代码没问题 - 这似乎是控件部分的某种“疏忽”(因为没有更好的词)。
我想我的问题是——对于你们所有的编码员,你们将如何处理这个问题?
this.webKitBrowser1.KeyPress += new KeyPressEventHandler(handleKeyPress);
this.webKitBrowser1.MouseMove += new MouseEventHandler(handleAction);
this.webKitBrowser1.MouseClick += new MouseEventHandler(handleAction);
this.webKitBrowser1.MouseDown += new MouseEventHandler(handleAction);
this.webKitBrowser1.MouseUp += new MouseEventHandler(handleAction);
this.webKitBrowser1.MouseDoubleClick += new MouseEventHandler(handleAction);
void handleKeyPress(object sender, KeyPressEventArgs e)
{
this.handleAction(sender, null);
}
void handleAction(object sender, MouseEventArgs e)
{
this.lastAction = DateTime.Now;
this.label4.Text = this.lastAction.ToLongTimeString();
}
更新
使用乔接受的解决方案,我整理了以下课程。感谢所有参与了的人。
class classIdleTime
{
[DllImport("user32.dll")]
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
internal struct LASTINPUTINFO
{
public Int32 cbSize;
public Int32 dwTime;
}
public int getIdleTime()
{
int systemUptime = Environment.TickCount;
int LastInputTicks = 0;
int IdleTicks = 0;
LASTINPUTINFO LastInputInfo = new LASTINPUTINFO();
LastInputInfo.cbSize = (Int32)Marshal.SizeOf(LastInputInfo);
LastInputInfo.dwTime = 0;
if (GetLastInputInfo(ref LastInputInfo))
{
LastInputTicks = (int)LastInputInfo.dwTime;
IdleTicks = systemUptime - LastInputTicks;
}
Int32 seconds = IdleTicks / 1000;
return seconds;
}
用法
idleTimeObject = new classIdleTime();
Int32 seconds = idleTimeObject.getIdleTime();
this.isIdle = (seconds > secondsBeforeIdle);