Thursday, February 10, 2005

Detect Windows Idle time

Here is a sample code of how you can detect idle time in Windows. The definition of idle time here refer to the time when there is no user interaction with Windows such as no keyboard and mouse input.

Detecting idle time is used in application like MSN Messenger to change the status to "Away" after the user does not interact with the Windows for a predefine time.

The enabler for this feature is the GetLastInputInfo() Win32 API and the LASTINPUTINFO Win32 structure.

1. Create a C# Winform project in Visual Studio .NET.
2. Drop a Timer, Label and Button onto the form.
3. Set the Timer Interval property to 1000(1 second).
4. Create a Click event handler for the button.
5. Create a Tick event handler for the timer.
6. Use the following code to complete the Winform code.
7. Then you can build the project and run the app.

8. When the application is running, click the button to enable to timer.
9. Stay your hand away from your keyboard and mouse.
10. You can see the lable is showing the number of seconds that the Windows has been idle.
11. The moment you move your mouse or press any key, the number is reset.


The sample code only serve as a mean to illustrate the concept and implementation. It has not been checked for minor bugs, quality and robustness.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace GetLastInput_Demo
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
static extern bool GetLastInputInfo(out LASTINPUTINFO plii);

[StructLayout( LayoutKind.Sequential )]
public static readonly int SizeOf =

public int cbSize;
public int dwTime;

private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Timer timer1;
private System.ComponentModel.IContainer components;

public Form1()
// Required for Windows Form Designer support


/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
if( disposing )
if (components != null)
base.Dispose( disposing );
#region Windows Form Designer generated code
private void InitializeComponent()
// Code is omitted here.

/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
Application.Run(new Form1());

private void button1_Click(object sender, System.EventArgs e)
timer1.Enabled = !timer1.Enabled;

private void timer1_Tick(object sender, System.EventArgs e)
int idleTime = 0;
lastInputInfo.cbSize = Marshal.SizeOf( lastInputInfo );
lastInputInfo.dwTime = 0;

int envTicks = Environment.TickCount;

if( GetLastInputInfo( out lastInputInfo ) )
int lastInputTick = lastInputInfo.dwTime;
idleTime = envTicks - lastInputTick;

int a;

if(idleTime > 0)
a = idleTime / 1000;
a = idleTime;

label1.Text = a.ToString();



At 2:58 AM, Blogger Cem Gündüz said...

thank you very much very good code.

program is running in my applicatin.

if i want this in windows enviroment, what can i do?

At 4:21 AM, Anonymous Anonymous said...

Thanks, I was looking for a way to detect if there is a user at a workstation and show the status on a website. Now I can create a systemtray or service app to upload this status to a website.

At 12:23 PM, Anonymous Mie-G said...

This help a lot!

At 6:13 PM, Blogger Orla said...

Just what I needed. Nice and simple


Post a Comment

<< Home