public static int GetLastInputTime() { int idleTime = 0; Win32Helper.LASTINPUTINFO lastInputInfo = new Win32Helper.LASTINPUTINFO(); lastInputInfo.cbSize = (UInt32)Marshal.SizeOf(lastInputInfo); lastInputInfo.dwTime = 0; int envTicks = Environment.TickCount; if (Win32Helper.GetLastInputInfo(ref lastInputInfo)) { int lastInputTick = (Int32)lastInputInfo.dwTime; idleTime = envTicks - lastInputTick; } return((idleTime > 0) ? (idleTime / 1000) : 0); }
public static Process GetForegroundProcess() { IntPtr hWnd; Process currentProcess = null; uint pid = 0; try { hWnd = Win32Helper.GetForegroundWindow(); Win32Helper.GetWindowThreadProcessId(hWnd, out pid); currentProcess = Process.GetProcessById((int)pid); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { hWnd = IntPtr.Zero; } return(currentProcess); }