Detecting if user is idle.


Detecting if user is idle.

procedure TForm1.Timer1Timer(Sender: TObject);

procedure idletask;
begin
  {Perform idle task here}
end;


var state:integer;
    loop:integer;
    idle,mousemove:boolean;
    newmousepos:tpoint;
begin
   idle:=true;   {Assume Idle}

   getCursorPos(newMousePos); {Get New mouse positions}
   if (oldmousepos.x<>newmousepos.x) or (oldmousepos.y<>newmousepos.y) then
     idle:=false;  {if difference then user is no longer idle}

   for loop:=0 to 255 do {Check if any keys are pressed}
      if getAsynckeystate(loop)=1 then
        idle:=false;

   if idle<>false then
   begin
      idlelength:=idlelength+1;   {continue counting how long idle in secs}
      label2.caption:=inttostr(idlelength);
   end else
   begin
     oldstate:=state;
     oldmousepos:=newmousepos;
     idlelength:=0;
     label2.caption:=inttostr(idlelength);
   end;
end;