Application and active time
I am trying to implement a CBT Hook to do one simple thing: Find out what application is active for how long? Active means "user is interacting with the application window".
For instance user working on proposal in MSWORD OR user surfing online on Youtube.
We need to find out the following:
- Name of the application.
- Name of the document or URL (if it is IE / Chrome / FF etc br开发者_C百科owsers).
- Active time of the application windows.
My approach was:
SetWindowsHookEx()
forWH_CBT
- In my callback function
CBTProcCallBack()
I check for codeHCBT_ACTIVATE
. I get the windows, application and exe details - Log the time.
However, not all the active windows get captured.
Is my approach ok OR am I going wrong?
Here is a simple solution to find out what all are the applications that user has opened. The approach is, say, you will have to check in a loop if the current foreground window has changed or not. try doing it in the timer event and if the previous window name is not same as current window name, you can easily calculate the time that user has used this application.
function ActiveCaption: string;
var
Handle: THandle;
Len: LongInt;
Title: string;
begin
result := '';
Handle := GetForegroundWindow;
if Handle <> 0 then
begin
Len := GetWindowTextLength(Handle) + 1;
SetLength(Title, Len);
GetWindowText(Handle, PChar(Title), Len);
ActiveCaption := TrimRight(Title);
end;
end;
精彩评论