Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell when Windows is inactive

Tags:

.net

windows

Various programs can do stuff only when you haven't used the computer for a while (eg screensaver, Google Desktop indexing, etc).

How do they know when it has been inactive? Is there some function in Windows that tells you how long it has been inactive, or do you have to use some kind of keyboard/mouse hook to track activity yourself?

I'm using C#, but I'm interested in any method of determining the inactivity.

like image 703
dan gibson Avatar asked Oct 15 '08 01:10

dan gibson


People also ask

How do you find out when Windows was activated?

Checking your activation status To check activation status in Windows 10, select the Start button, and then select Settings > Update & Security and then select Activation . Your activation status will be listed next to Activation. You are activated.

What is inactive version of Windows?

A window on screen that is not currently selected. Its title bar is typically grayed out. In Windows, pressing Alt-Tab switches between all active and inactive windows. Contrast with active window.

How do I know if Windows 10 is activated permanently?

Step 1: Press Win + R keyboard shortcut to quickly open Run dialog box. Step 2: Type the run command: slmgr. vbs –xpr and click OK. Then it will display a new dialog box telling if your Windows 10 is permanently activated or not activated, or the activation will expire someday.

What does inactive mean on a computer?

An inactive window is any window that is not being used or in the background of the active window.


1 Answers

EDIT: changed answer, providing text and detail behind Shy's answer (which should be and was accepted). Feel free to merge and delete this one.

GetLastInputInfo Function The GetLastInputInfo function retrieves the time of the last input event.

Pasted here from P/Invoke

This function retrieves the time since last user input

[DllImport("user32.dll")]
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);

static int GetLastInputTime()
{
    int idleTime = 0;
    LASTINPUTINFO lastInputInfo = new LASTINPUTINFO();
    lastInputInfo.cbSize = Marshal.SizeOf( lastInputInfo );
    lastInputInfo.dwTime = 0;

    int envTicks = Environment.TickCount;

    if( GetLastInputInfo( ref lastInputInfo ) )
    {
    int lastInputTick = lastInputInfo.dwTime;

    idleTime = envTicks - lastInputTick;
    }

    return (( idleTime > 0 ) ? ( idleTime / 1000 ) : idleTime );
}

[StructLayout( LayoutKind.Sequential )]
struct LASTINPUTINFO
{
    public static readonly int SizeOf = Marshal.SizeOf(typeof(LASTINPUTINFO));

    [MarshalAs(UnmanagedType.U4)]
    public int cbSize;    
    [MarshalAs(UnmanagedType.U4)]
    public UInt32 dwTime;
}

FWIW: I implemented a global keyboard and mouse hook during AnAppADay. See this app for the source - it's pretty close to what you want. The classes you'll want are in the AnAppADay.Utils namespace. [scratched due to linkrot]

like image 195
TheSoftwareJedi Avatar answered Nov 10 '22 06:11

TheSoftwareJedi