From ea688c0ab0ef795f63cbcf77dfaf8fe048bee2e7 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Fri, 27 Feb 2026 00:11:42 -0500 Subject: [PATCH] feat(watcher): add GetLastInputInfo P/Invoke for idle detection Co-Authored-By: Claude Opus 4.6 --- WindowWatcher/NativeMethods.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/WindowWatcher/NativeMethods.cs b/WindowWatcher/NativeMethods.cs index 20c319a..b541419 100644 --- a/WindowWatcher/NativeMethods.cs +++ b/WindowWatcher/NativeMethods.cs @@ -13,4 +13,22 @@ internal static partial class NativeMethods [LibraryImport("user32.dll", SetLastError = true)] internal static partial uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId); + + [StructLayout(LayoutKind.Sequential)] + internal struct LASTINPUTINFO + { + public uint cbSize; + public uint dwTime; + } + + [DllImport("user32.dll")] + internal static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); + + internal static TimeSpan GetIdleTime() + { + var info = new LASTINPUTINFO { cbSize = (uint)Marshal.SizeOf() }; + if (!GetLastInputInfo(ref info)) + return TimeSpan.Zero; + return TimeSpan.FromMilliseconds(Environment.TickCount64 - info.dwTime); + } }