धन्यवाद लॉर्डकॉवर। यह कोड here से है। यह कक्षा आपके लिए कीबोर्ड और माउस नियंत्रण का नियंत्रण लेती है।
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;
}
}
स्रोत
2012-10-27 15:57:54
समाधान GetLastInputInfo के आधार पर आम तौर पर बेहतर कर रहे हैं, क्योंकि यह हुक इंजेक्शन लगाने की आवश्यकता नहीं है: आप इस तरह एक टाइमर में उपयोग कर सकते हैं हर प्रक्रिया में कोड। –