Windows Develop Bookmark and Share   
 index > Windows Forms General > detect system wide user idle time
 

detect system wide user idle time

How can I get the time a user has been idle (i.e didn'tperform any input) system wide? Similar like the screensaver or Windows Live Messenger does it. Even better would be to get notified via an event when the idle time has exceeded a specific timespan. Maybe there is something for that in WMI ?
bitbonk  Wednesday, May 23, 2007 11:26 AM
You can P/Invoke the GetLastInputInfo() API function. Check this thread for code to detect the screen saver turning on. Here's a sample app that demonstrates the API function:

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

namespace WindowsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
timer1.Interval = 1000;
timer1.Tick += CheckLastInput;
timer1.Enabled = true;
}
private void CheckLastInput(object sender, EventArgs e) {
LastInputInfo info = new LastInputInfo();
info.cbSize = Marshal.SizeOf(info);
GetLastInputInfo(ref info);
uint tick = GetTickCount();
uint msec = tick - info.dwTime;
Console.WriteLine("Last input was {0} milliseconds ago", msec);
}
[StructLayout(LayoutKind.Sequential)]
private struct LastInputInfo {
public int cbSize;
public uint dwTime;
}
[DllImport("user32.dll")]
private static extern bool GetLastInputInfo(ref LastInputInfo info);
[DllImport("kernel32.dll")]
private static extern uint GetTickCount();
}
}
nobugz  Wednesday, May 23, 2007 12:38 PM
You can P/Invoke the GetLastInputInfo() API function. Check this thread for code to detect the screen saver turning on. Here's a sample app that demonstrates the API function:

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

namespace WindowsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
timer1.Interval = 1000;
timer1.Tick += CheckLastInput;
timer1.Enabled = true;
}
private void CheckLastInput(object sender, EventArgs e) {
LastInputInfo info = new LastInputInfo();
info.cbSize = Marshal.SizeOf(info);
GetLastInputInfo(ref info);
uint tick = GetTickCount();
uint msec = tick - info.dwTime;
Console.WriteLine("Last input was {0} milliseconds ago", msec);
}
[StructLayout(LayoutKind.Sequential)]
private struct LastInputInfo {
public int cbSize;
public uint dwTime;
}
[DllImport("user32.dll")]
private static extern bool GetLastInputInfo(ref LastInputInfo info);
[DllImport("kernel32.dll")]
private static extern uint GetTickCount();
}
}
nobugz  Wednesday, May 23, 2007 12:38 PM
Thanks a Lot,

Saved a lot of time.
aecsant  Wednesday, July 01, 2009 6:15 AM
I'd like to know how Screensavers do it because this isn't it. Close but no cigar (for my problem).
BillJam  Wednesday, October 07, 2009 9:47 PM

You can use google to search for other answers

Custom Search

More Threads

• Bold Specific Words
• DataGridView get/set scrollbar position
• Copy and Paste Keyboard Shortcuts won't work in Rich Text Box.
• Simple SQL Question - Issue with brackets in INSERT String
• Form cancel button
• Key Board Shortcuts and the MenuStrip Control
• How to activate main form after splash screen
• DataGrid Button column color
• Wanting to keep a control static
• Will I mess anything up if I delete this from the designer.cs file ?