Solved

Detecting idle state

Posted on 2000-04-21
9
282 Views
Last Modified: 2010-04-04
Can somebody give me a full example on how to install some sort of hook that will execute a function after 2 minutes of idle time ?
0
Comment
Question by:frenzyr
  • 3
  • 2
  • 2
  • +2
9 Comments
 

Expert Comment

by:SuperSy
ID: 2739200
I am not exactly sure about your definition of "idle time", but I am going to go ahead and assume that you mean that the your application is not doing anything.  (You could be moving the cursor or pressing the Shift key, as long as your application is doing nothing.)

1) Have a TTimer component in your application

(ex.)
....
type
  TForm1 = class(TForm)
  ...
  IdleTimer: TTimer;
  ...
end;
....

2) In your application's OnCreate, define your OnIdle handling procedure.

(ex.)
procedure TForm1.FormCreate(Sender: TObject);
begin
  ...
  Application.OnIdle:= MyIdleHandler;
  ..
end;

3) In your OnIdle handler, set the timer to two minutes when called. Set Done to True so that your CPU is not eaten up.

(ex.)
procedure TForm1.MyIdleHandler(Sender: TObject; var Done: Boolean);
begin
  Done := True;
  IdleTimer.Interval := 2 * 60 * 1000;
  // Two minutes in msec.
  IdleTimer.Enabled := True;
end;

4) Finally, in your OnTimer event handler for your Idle time timer, do what you want to do.

procedure TForm1.IdleTimerTimer(Sender: TObject);
begin
  IdleTimer.Enabled := False;
  IdleTimer.Interval := 0;
  ShowMessage(Two minutes idle time passed!');  
end;

This works...if your definition of "idle time" matches mine.  

^_^

SuperSy

0
 

Expert Comment

by:SuperSy
ID: 2739208
If you want to update the timer when a key is pressed or the mouse is moved, you can set an OnMessage handler just like the OnIdle handler.

In 2) above, add
  Application.OnMessage := MyMessageHandler;

and write your own handler like the one below.

procedure TForm1.MyMessageHandler(var Msg: TMsg; var Handled: Boolean);
begin
  if (Msg.message= WM_KEYDOWN) or (Msg.message= WM_MOUSEMOVE) then
  begin
    IdleTimer.Interval := 2 * 60 * 1000;
    // Two minutes in msec.
    IdleTimer.Enabled := True;
  end;    
end;
0
 
LVL 1

Author Comment

by:frenzyr
ID: 2739567
Thank you for your comment, however i mean a system wide idle state, instead of an application wide one.

Can you plesae help me with that ?
0
 
LVL 3

Expert Comment

by:bryan7
ID: 2740809
i have a demo of an app that monitors cpu usage..
you can use that .. if 2 minutes have happened with the cpu usage at 0...1 .. then do something
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Accepted Solution

by:
msedi earned 200 total points
ID: 2750994
Hello,

I don't know if you still want an answer. Here's mine you should set up a windows hook in a dll, because only there the hook is global. After that you should install a hook for every event that comes up (e. g. messages, mouse and so on). Just set a timer if a message occurs and reset the timer every time another message occurs. If the time is the given time you can call your method. If I have time I will set up a demo.

Good luck, Martin
0
 
LVL 1

Author Comment

by:frenzyr
ID: 2754730
Thanks i've done that and its working now.
0
 

Expert Comment

by:clopez
ID: 2807150
I am searching for a solution for the same problem.  But I don't know how to implement it, could you explain futher please.

If you can post an example or send it to clopez@intercom.es

Thanks
0
 
LVL 3

Expert Comment

by:bryan7
ID: 2809095
me too.. brian7@teleline.es

thanks
0
 
LVL 3

Expert Comment

by:bryan7
ID: 2809097
me too.. brian7@teleline.es

thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now