Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Is is possible to determine user has no activity for a specific time in windows, like screen savers do ????
Posted
Comments
Lakamraju Raghuram 26-Jan-12 1:16am    
A 5 for you, because I learned a new concept today
Sergey Alexandrovich Kryukov 26-Jan-12 1:26am    
Sorry, you did not... not just yet. I tried to explain :-)
--SA
Lakamraju Raghuram 26-Jan-12 2:29am    
ok. understood

You need to catch all input events on a system-wide level. This is done using Windows Hooks. Start here:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms632589%28v=vs.85%29.aspx[^].

You can only install the global (system-global hook) in a DLL using SetWindowsHookEx. The code of the hook should communicate with the host application. You should understand that the hook handler code does not have to belong to your thread or process, so you should use some inter-process communication to access event data.

As far as I understand your problem, I would use global named event objects. You only need to wait for some first input event. You can create some thread in the host application and put it to wait state by the call to WaitForSignleObject on the handle of the event object. The OS will switch the calling thread off and won't schedule it back to execution until it is waken up by the signaled event. Signal the same event in your hooked event handler. The thread will wake up and do some action which you should call in the next line.

Please see:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682396%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms682400%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686211%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms685081%28v=vs.85%29.aspx[^],
http://msdn.microsoft.com/en-us/library/windows/desktop/ms687032%28v=vs.85%29.aspx[^].

See the article referenced in the last link (for example) for other synchronization primitives.

Read the event object and overview: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682655%28v=vs.85%29.aspx[^].

—SA
 
Share this answer
 
I think you should use GetLastInputInf(...) method. Look at the link below:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms646302(v=vs.85).aspx[^]

Hope this solved your issue
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 26-Jan-12 1:25am    
Sorry, absolutely wrong. It cannot trigger anything as OP needs. It will give you the information on the event that was the last on in the past, only when you call it. Polling anything in the system periodically is not an option, this would be a crime, virus, malware, foolishness, waste of resource, system degradation, etc.

I provided the recipe, I think this is the only way.
--SA
tolw 26-Jan-12 3:21am    
I disagree. As with all things - it depends on the usage. The OP mentioned screensavers. So I'm guessing that he does not need to determine Idle states measured in seconds, but rather in minutes. If so, than pooling every couple of seconds does not in my opinion pose a great threat on the system. I'm not saying it is always acceptable, I am saying that because it is much simpler - it may be a better solution than hooks in some cases. And for the simplicity Lakamraju Raghuram gets my 5.
Lakamraju Raghuram 26-Jan-12 5:16am    
Any way I understood few concepts today.
Thanks tolw.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900