Back


unit ETimer;
(*  Enhanced Timer V1.00 - released June 09, 1998 Copyright (c) 1998 by Demian

Disclaimer:
This component is distributed as Freeware. There is no charge or warranty  whatsoever. The author will not be held responsible for any direct or indirect  damage resulting from the use of the component or any derivatives
thereof.
In short: USE IT AT YOUR OWN RISK! You are allowed to include the component
code in any commercial or non commercial product. You are also allowed to make  as many copies of the component as you want and distribute them, as long as you do not receive any money for the copying/distribution.

You can send any comments, suggestions and bugs to demian@bhnet.com.br
New versions can be found at: http://www.bhnet.com.br/~demian
 
http://web.unix.horizontes.com.br/~demian

If you do any modification to this code, please send me a copy.
If you use this code on any 'real-life' application, please let me know.
If you think this code is completely useless, please DON'T LET ME KNOW!

What it does:
TTimer descendent with a new property, Snooze, that keeps a counter of miliseconds elapsed since the last time the user moved/clicked the mouse or pressed a key. Useful to implement timeout routines.

 How to use it:
 1. Install the component [VNM Pallete];
 2. Drop one or more TETimer onto a form;
 3. Use the component as you would use a regular TTimer. The only difference is the new Snooze property, that can be consulted/edited at run-time.
For example, the following code (OnTimer event) emits a sound whenever the application stays idle for more than one minute:

    procedure TForm1.ETimer1Timer(Sender: TObject);
    begin
        with TETimer(Sender) do
           if Snooze > 60000 then begin
           MessageBeep(MB_OK);
           Snooze := 0;
      end;
    end; *)

interface

uses Classes,ExtCtrls;

type
  TETimer = class(TTimer)
  private
    function GetSnooze: LongInt;
    procedure SetSnooze(const Value: LongInt);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property Snooze: LongInt read GetSnooze write SetSnooze;
end;

procedure Register;

implementation

uses {$IFDEF WIN32}Windows;{$ELSE}WinProcs,WinTypes;{$ENDIF}

var
  Instances: integer;
  ElapsedTime: LongInt;
  whKeyboard,whMouse: HHook;

{______________________________________________________________________________}
procedure Register;
begin
    RegisterComponents('VNM',[TETimer]);
end;

{______________________________________________________________________________}
{$IFDEF WIN32}
function MouseHookCallBack(Code: integer; Msg: WPARAM;
                           MouseHook: LPARAM): LRESULT; stdcall;
{$ELSE}
function MouseHookCallBack(Code: integer; Msg: word;
                           MouseHook: longint): longint; export;
{$ENDIF}
begin
  if Code >= 0 then
    ElapsedTime := GetTickCount;
  Result := CallNextHookEx(whMouse,Code,Msg,MouseHook);
end;

{______________________________________________________________________________}
{$IFDEF WIN32}
function KeyboardHookCallBack(Code: integer; Msg: WPARAM;
                              KeyboardHook: LPARAM): LRESULT; stdcall;
{$ELSE}
function KeyboardHookCallBack(Code: integer; Msg: word;
                              KeyboardHook: longint): longint; export;
{$ENDIF}
begin
  if Code >= 0 then
    ElapsedTime := GetTickCount;
  Result := CallNextHookEx(whKeyboard,Code,Msg,KeyboardHook);
end;

{______________________________________________________________________________}
constructor TETimer.Create(AOwner:TComponent);
  function GetModuleHandleFromInstance: THandle;
  var
    s: array[0..512] of char;
  begin
    GetModuleFileName(hInstance,s,sizeof(s)-1);
    Result := GetModuleHandle(s);
  end;
begin
  inherited Create(AOwner);
  Inc(Instances);
  if Instances = 1 then begin
    ElapsedTime := GetTickCount;
    whMouse := SetWindowsHookEx(WH_MOUSE,MouseHookCallBack,
                                GetModuleHandleFromInstance,
                                {$IFDEF
WIN32}GetCurrentThreadID{$ELSE}
                                              GetCurrentTask{$ENDIF});
    whKeyboard := SetWindowsHookEx(WH_KEYBOARD,KeyboardHookCallBack,
                                   GetModuleHandleFromInstance,
                                   {$IFDEF
WIN32}GetCurrentThreadID{$ELSE}
 
GetCurrentTask{$ENDIF});
  end;
end;

{______________________________________________________________________________}
destructor TETimer.Destroy;
begin
  Dec(Instances);
  if Instances = 0 then begin
    UnhookWindowsHookEx(whKeyboard);
    UnhookWindowsHookEx(whMouse);
  end;
  inherited Destroy;
end;

{______________________________________________________________________________}
procedure TETimer.SetSnooze(const Value: LongInt);
begin
  ElapsedTime := GetTickCount + Value;
end;

{______________________________________________________________________________}
function TETimer.GetSnooze: LongInt;
begin
  result := GetTickCount - ElapsedTime;
end;

end.


Back