私は比較的単純なC#プロジェクトを書いています。「パブリックWebターミナル」を考えてください。基本的に、フィルドックされた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);