public static uint GetLastInputInfoTickCount() { NativeMethods.LASTINPUTINFO lastInputInfo = new NativeMethods.LASTINPUTINFO(); lastInputInfo.cbSize = (uint)Marshal.SizeOf(lastInputInfo); lastInputInfo.dwTime = 0; return(NativeMethods.GetLastInputInfo(ref lastInputInfo) ? lastInputInfo.dwTime : 0); }
public static bool ShouldShowPopup(System.Windows.Forms.Form popupForm, Screen popupScreen) { // Is the current session locked? if (_sessionLocked) { return(false); } // Has the last mouse movement or keyboard action been too long ago? var lii = new NativeMethods.LASTINPUTINFO(); lii.cbSize = (uint)Marshal.SizeOf(lii); bool fResult = NativeMethods.GetLastInputInfo(ref lii); if (!fResult) { throw new Exception("GetLastInputInfo failed"); } if (NativeMethods.GetTickCount() - lii.dwTime > IdleTime) { return(false); } // Only consider the foreground window when it is on the same monitor // as the popup is going to be displayed on. var hForeground = NativeMethods.GetForegroundWindow(); var screen = Screen.FromHandle(hForeground); if (screen.WorkingArea != popupScreen.WorkingArea) { return(true); } // Is the foreground application running in full-screen mode? NativeMethods.RECT rcForeground = new NativeMethods.RECT(); NativeMethods.GetClientRect(hForeground, ref rcForeground); var foreground = ClientToScreen(hForeground, rcForeground); // If the client rect is covering the entire screen, the application is a // full-screen application. return(!( screen.Bounds.Left >= foreground.Left && screen.Bounds.Top >= foreground.Top && screen.Bounds.Right <= foreground.Right && screen.Bounds.Bottom <= foreground.Bottom )); }
public override void Start() { try { // Register for Window Events _dele = new NativeMethods.WinEventDelegate(WinEventProc); _hWinEventHookForWindowSwitch = NativeMethods.SetWinEventHook(NativeMethods.EVENT_SYSTEM_FOREGROUND, NativeMethods.EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, _dele, 0, 0, NativeMethods.WINEVENT_OUTOFCONTEXT); _hWinEventHookForWindowTitleChange = NativeMethods.SetWinEventHook(NativeMethods.EVENT_OBJECT_NAMECHANGE, NativeMethods.EVENT_OBJECT_NAMECHANGE, IntPtr.Zero, _dele, 0, 0, NativeMethods.WINEVENT_OUTOFCONTEXT); // Register for logout/shutdown event SystemEvents.SessionEnding += SessionEnding; SystemEvents.PowerModeChanged += OnPowerChange; // Register to check if idle or not if (Settings.RecordIdle) { // reset everything properly if (_idleCheckTimer != null || _idleSleepValidator != null) { Stop(); } // register for events _idleCheckTimer = new Timer(); _idleCheckTimer.Interval = Settings.IdleTimerInterval_ms; _idleCheckTimer.Elapsed += CheckIfIdleTime; _idleCheckTimer.Start(); _idleSleepValidator = new Timer(); _idleSleepValidator.Interval = Settings.IdleSleepValidate_TimerInterval_ms; _idleSleepValidator.Elapsed += ValidateSleepIdleTime; _idleSleepValidator.Start(); _lastInputInfo = new NativeMethods.LASTINPUTINFO(); _lastInputInfo.cbSize = (uint)Marshal.SizeOf(_lastInputInfo); _lastInputInfo.dwTime = 0; } IsRunning = true; } catch (Exception e) { Database.GetInstance().LogWarning("Registering events failed: " + e.Message); IsRunning = false; } }
public static TimeSpan GetIdleTimeSpan() { int systemUptime = Environment.TickCount; int lastInputTicks = 0; int idleTicks = 0; NativeMethods.LASTINPUTINFO lastInputInfo = new NativeMethods.LASTINPUTINFO(); lastInputInfo.cbSize = (uint)Marshal.SizeOf(lastInputInfo); lastInputInfo.dwTime = 0; if (NativeMethods.GetLastInputInfo(ref lastInputInfo)) { lastInputTicks = (int)lastInputInfo.dwTime; idleTicks = systemUptime - lastInputTicks; return new TimeSpan(0, 0, 0, 0, idleTicks); } else { return TimeSpan.MinValue; } }
public static TimeSpan GetLastInputTime() { int idleTime = 0; try { var lastInputInfo = new NativeMethods.LASTINPUTINFO(); lastInputInfo.cbSize = Marshal.SizeOf(lastInputInfo); lastInputInfo.dwTime = 0; if (NativeMethods.GetLastInputInfo(ref lastInputInfo)) { int lastInputTick = lastInputInfo.dwTime; idleTime = Environment.TickCount - lastInputTick; } } catch (Exception ex) { Logger?.Here().Error(ex.Message); } return(TimeSpan.FromMilliseconds(idleTime)); }
private void tmrIdle_Tick(object sender, EventArgs e) { // Get the system uptime int systemUptime = Environment.TickCount; // The tick at which the last input was recorded uint LastInputTicks = 0; // The number of ticks that passed since last input uint IdleTicks = 0; // Set the struct NativeMethods.LASTINPUTINFO LastInputInfo = new NativeMethods.LASTINPUTINFO(); LastInputInfo.cbSize = (uint)Marshal.SizeOf(LastInputInfo); LastInputInfo.dwTime = 0; // If we have a value from the function if (NativeMethods.GetLastInputInfo(ref LastInputInfo)) { // Get the number of ticks at the point when the last activity was seen LastInputTicks = LastInputInfo.dwTime; // Number of idle ticks = system uptime ticks - number of ticks at last input IdleTicks = (uint)systemUptime - LastInputTicks; } // divide by 1000 to transform the milliseconds to seconds and by 60 to convert to minutes _idleTime = (IdleTicks / 1000); if (_idleTime == 0 && _timersEnabled == false) { InitialiseTimers(this.WritingEnabled); } else if (_idleTime >= _disableTimersAfter && _timersEnabled && chkTurnOffWhenUserInactive.Checked) { InitialiseTimers(false); } //this.Text = _iIdleTime.ToString(); }
public override void Start() { try { // Register for Window Events _dele = new NativeMethods.WinEventDelegate(WinEventProc); _hWinEventHookForWindowSwitch = NativeMethods.SetWinEventHook(NativeMethods.EVENT_SYSTEM_FOREGROUND, NativeMethods.EVENT_SYSTEM_FOREGROUND, IntPtr.Zero, _dele, 0, 0, NativeMethods.WINEVENT_OUTOFCONTEXT); _hWinEventHookForWindowTitleChange = NativeMethods.SetWinEventHook(NativeMethods.EVENT_OBJECT_NAMECHANGE, NativeMethods.EVENT_OBJECT_NAMECHANGE, IntPtr.Zero, _dele, 0, 0, NativeMethods.WINEVENT_OUTOFCONTEXT); // Register to check if idle or not if (Settings.RecordIdle) { if (_idleCheckTimer != null) Stop(); _idleCheckTimer = new Timer(); _idleCheckTimer.Interval = Settings.IdleTimerIntervalInMilliseconds; _idleCheckTimer.Elapsed += CheckIfIdleTime; _idleCheckTimer.Start(); _lastInputInfo = new NativeMethods.LASTINPUTINFO(); _lastInputInfo.cbSize = (uint)Marshal.SizeOf(_lastInputInfo); _lastInputInfo.dwTime = 0; } IsRunning = true; } catch (Exception e) { Database.GetInstance().LogWarning("Registering events failed: " + e.Message); IsRunning = false; } }