Solved

Detecting idle state

Posted on 2000-04-21
9
280 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Accepted Solution

by:
msedi earned 200 total points
Comment Utility
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
Comment Utility
Thanks i've done that and its working now.
0
 

Expert Comment

by:clopez
Comment Utility
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
Comment Utility
me too.. brian7@teleline.es

thanks
0
 
LVL 3

Expert Comment

by:bryan7
Comment Utility
me too.. brian7@teleline.es

thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 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

7 Experts available now in Live!

Get 1:1 Help Now