Related links:
Very important:
Monitor all key and mouse input
http://stackoverflow.com/questions/744980/hide-mouse-cursor-after-an-idle-time/745227#745227
Monitor key and mouse input for special application
http://www.codeproject.com/KB/WPF/AutologoffWPF.aspx?msg=3330342#xx3330342xx
Some others:
http://stackoverflow.com/questions/1111615/getting-inactivity-idle-time-in-a-wpf-application
http://www.syncfusion.com/faq/wpf/wpf_c59c.aspx
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/44406a8d-6762-421d-9fcb-b1bc23cd9f1a
Monitor key and mouse input for special application sample
http://www.codeproject.com/KB/WPF/AutologoffWPF/AutoLogoffInWPF.zip
If you are using WinForms and will only deploy on Windows machines then it's quite easy to use user32 GetLastInputInfo to handle both mouse and keyboard idling.
public static class User32Interop
{
public static TimeSpan GetLastInput()
{
var plii = new LASTINPUTINFO();
plii.cbSize = (uint)Marshal.SizeOf(plii);
if (GetLastInputInfo(ref plii))
return TimeSpan.FromMilliseconds(Environment.TickCount - plii.dwTime);
else
throw new Win32Exception(Marshal.GetLastWin32Error());
}
[DllImport("user32.dll", SetLastError = true)]
static extern bool GetLastInputInfo(ref LASTINPUTINFO plii);
struct LASTINPUTINFO
{
public uint cbSize;
public uint dwTime;
}
}
And then in your Form
public partial class MyForm : Form
{
Timer activityTimer = new Timer();
TimeSpan activityThreshold = TimeSpan.FromMinutes(2);
bool cursorHidden = false;
public Form1()
{
InitializeComponent();
activityTimer.Tick += activityWorker_Tick;
activityTimer.Interval = 100;
activityTimer.Enabled = true;
}
void activityWorker_Tick(object sender, EventArgs e)
{
bool shouldHide = User32Interop.GetLastInput() > activityThreshold;
if (cursorHidden != shouldHide)
{
if (shouldHide)
Cursor.Hide();
else
Cursor.Show();
cursorHidden = shouldHide;
}
}
}
本文介绍了一种监测用户键盘和鼠标活动的方法,特别适用于Windows平台上的WinForms应用。通过使用user32.dll中的GetLastInputInfo函数,可以获取用户的最后输入时间,并据此判断用户是否处于空闲状态。
916

被折叠的 条评论
为什么被折叠?



