tags:

views:

346

answers:

5

How can I detect if an application is not used for more than x minutes in DELPHI

A: 

Depends on how you're defining "used" -- if you were monitoring yourself, you could look at the last time you responded to user interaction by logging it when it happened (mouse move/key pressed/menu event fired/etc.). Monitoring another application is tricky as it'll be harder to define that it is "in use".

Rowland Shaw
YesJust the user inside my application. I need to detect him not using the application for x minutes
Jlouro
A: 

That really depends on the application and what it does. While users may not interact with it in the sense of new input, they certainly might be viewing the client area that is visible.

Also - you don't say if you want to detect this internal to the app or external to the app.

Simple methods

  • see if it has current focus.
  • check if the window is visible

lots of others too, but they rely on the app itself.

You must define what you mean by "used" as well. It could mean different things and that would make significant changes to how you determine whether it met your criteria or not.

Tim
I need to be able to detect if the user is not using it for more than x minutes, to be able to perform some “housekeeping” actions.
Jlouro
+2  A: 

Here is some code that looks for mouse and keybord activity with the applicatin

procedure TUserActivity.AppMessage(var Msg: TMsg; var Handled: Boolean);
begin
  Handled := False;
  case Msg.message Of
    WM_KEYDOWN,
    WM_LBUTTONDOWN,
    WM_MBUTTONDOWN,
    WM_RBUTTONDOWN:
      Activity := TRUE;
    WM_MOUSEMOVE:
    begin
      if (LastXYPos <> Msg.lParam) then 
        Activity := True;
      LastXYPos := Msg.lParam;
    end;
  end;
end;
Charles Faiga
If you wanted to do something after a certain amount of inactivity you could reset a timer each time there is an activity.
Lars Truijens
+3  A: 

If you write Windows app take a look at GetLastInputInfo function.

aku
Very nice. All code I've seen (or have written) doing the tracking of user input was essentially doing what this one API call seems to do... It's really much better than hooking into mouse and keyboard handling of the Application object.
mghie
Any suggestions when you should check if the time expired? With a timer?
Lars Truijens
Oh, and this is session wide. It won't help if you wanted to know if *your* application is in use. If someone is using MSWord instead of while your application is open on the background this function will still return activity.
Lars Truijens
I don't know about the needs of the OP (his question is awfully vague), but I consider the fact that it is session-wide a plus for my use case. I don't want to log out the user just because he's for example reading the documentation of my program in a PDF viewer, I want to do that only if he's...
mghie
actually away from the system, doing something else entirely. This API function will work perfectly for me, I didn't actually know it yet. And you could actually parse the "if an application" instead of "if my application" as the OP wanting the same. Maybe he should edit the question.
mghie
Ok here goes: It’s a management application. And I need to be able to detect if the user is not using it for more than x minutes, to be able to perform some “housekeeping” actions. He can be using other application as long as ”my” application as not the focus.
Jlouro
+1  A: 

Use the applications OnDeactivate and onActive events.. That way you can abort the longrunning job if the user activates your program again.

ex:

 Application.OnDeactivate = yourDeactivProcedure;

procedure mainform.YourDecativateProcedure (sender : tObject);
begin
 // do your job.. 
end;

To handle the activate event to abort you either have to do it a bad way with a sleep and after the sleep check if i global vairable has been set. Or you can have a theared object that does the loongrunning job. Which I would say is much better. You can set the loongrunningjobs priority to low and it wont affect your program as much,

Richard L