我需要創建一個程序來監視計算機的活動。如鼠標移動,鼠標點擊或鍵盤輸入。我不需要記錄計算機正在使用的情況。如果他們的計算機在一段時間內沒有被使用,即15分鐘,我需要發起一個事件。檢測用戶活動
有沒有辦法讓我知道這些事件?
我需要創建一個程序來監視計算機的活動。如鼠標移動,鼠標點擊或鍵盤輸入。我不需要記錄計算機正在使用的情況。如果他們的計算機在一段時間內沒有被使用,即15分鐘,我需要發起一個事件。檢測用戶活動
有沒有辦法讓我知道這些事件?
檢查this article它可以獲得計算機的空閒時間,然後你會在任意條件下觸發你的事件。
僞代碼:
If Computer_is_Idle > 15 minutes Then
Do this
Else
Do that or Wait more...
注:來源的文章中可用的代碼。
謝謝LordCover。此代碼從here。該類爲您控制鍵盤和鼠標控件。您可以在定時器使用這樣的:基於GetLastInputInfo
private void timer1_Tick(object sender, EventArgs e)
{
listBox1.Items.Add(Win32.GetIdleTime().ToString());
if (Win32.GetIdleTime() > 60000) //1minute
{
textBox1.Text = "SLEEPING NOW";
}
}
//主代碼控制,粘貼到您的表單代碼
internal struct LASTINPUTINFO
{
public uint cbSize;
public uint dwTime;
}
public class Win32
{
[DllImport("User32.dll")]
public static extern bool LockWorkStation();
[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);
}
public static long GetTickCount()
{
return Environment.TickCount;
}
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;
}
}
解決方案通常更好,因爲它不需要注射鉤代碼到每個進程中。 – 2013-07-08 23:48:29