我在寫一個相對簡單的C#項目。認爲「公共Web終端」。從本質上來說,它有一個最大化的形式,其上有一個填充停靠的Web瀏覽器。我使用的Web瀏覽器控件是WebKit的控制在這裏找到:.NET - 通過頑固控制檢測鼠標移動
我試圖通過保持它代表鼠標移動或按鍵被做了最後一次的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();
}
UPDATE
使用喬接受的解決方案,我總結了以下類。感謝所有參與了的人。
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);
實現並像夢一樣工作。我在原文中添加了我放在一起的類的代碼。 – Dutchie432 2010-08-26 12:55:05