I saw a lot of posts about calculating idle time within a program, but what I am trying to do is calculate the idle time of a separate Windows program. Long story short, we have 12 licenses for very expensive accounting software. We have about 20 employees, but not everyone uses the software. From time to time, we will have all 12 licenses being used -- and there is a hierarchy in the firm as to who should be able to access the software based upon need.
I am being tasked to write a program that will let us "kick off" users. I wrote a simple program that will run in the background and check if the software is loaded. Another program simply sends a command through a shared network drive to tell the program to close if necessary.
It works. What I'm being asked to do now is to include the idle time of the software. Is there any way for me to monitor an external application for keystrokes or mouse clicks? I think I can basically report the last time the application was used and derive the idle time from subtracting it from the current time. However, I'm struggling with how to figure out if any keys were sent to the specific application? Logically I'm thinking this is like a keylogger (without the logging), but only if the specific application is in focus.
Does anyone have any ideas I can explore? I'm willing to do the research, but just wondering if anyone knows of any API or other tricks to use.
You can combine GetForgroundWindow
and GetLastInputInfo
(Win32 functions) in a reasonable loop.
If the target application (target) is running, and the target is not the foreground window then we can assume that the application is idle. Just record the time it transitioned from foreground to background.
If the target is the foreground window then you can use GetLastInputInfo
to determine the last time the application received mouse or keyboard input.
** Unfortunately MSDN is down right now so I can't link to the documentation.