Re: mouse idle timer

Posted by Harry D. P16510 on December 19, 2008

In Reply to mouse idle timer posted by Richard1963 on November 25, 2008

: Hello i have made an idle timer which counts until the mouse is moved and then resets the counter to zero.
: Til so far no problem, but i want to use ShowCursor(false) and ShowCursor(True) to hide and show the mouse pointer when the mouse is not moved. i am a beginner. please help
: this is my code:
:
: function SecondsIdle: DWord;
: var
:   liInfo: TLastInputInfo;
: begin
:   liInfo.cbSize := SizeOf(TLastInputInfo) ;
:   GetLastInputInfo(liInfo) ;
:   Result := (GetTickCount - liInfo.dwTime) DIV 1000
: end;

: procedure TfrmJukebox.Timer1Timer(Sender: TObject);
: begin
:   If Button2.Caption > '20' Then begin
:     Button2.Caption := Format('%d', [SecondsIdle]);
:     Button1.Caption := ' muis UIT ' ;
:     ShowCursor(false);
:   end
:   else begin
:     { if movement mouse on}
:     Button2.Caption := Format('%d', [SecondsIdle]);
:     Button1.Caption := ' muis AAN ' ;
:     ShowCursor(True)
:   end;
: end;
:
: it doesnt work like it should, looks like randomly on and off or is it simply to slow?

Nice idea.
It isn't too slow, but the code isn't entirely correct. Function SecondsIdle is OK, but change the source code for Timer1Timer as follows:

procedure  TfrmJukebox.Timer1Timer(Sender: TObject);
begin
  Button2.Caption := Format('Seconds idle: %d', [SecondsIdle]);
  if SecondsIdle > 20 then begin
    { no user input for more than 20 seconds }
    Button1.Caption := ' cursor OFF ' ;
    Screen.Cursor := crNone;
  end
  else begin
    { user input less than 20 seconds ago }
    Button1.Caption := ' cursor ON ' ;
    Screen.Cursor := crDefault;
  end;
end; 

By the way, your SecondsIdle gives the time interval not only since the last time that the mouse has moved, but also since the mouse was clicked or a key was pressed on the keyboard (maybe also other input actions, I didn't check for other things).

Good luck!
Harry

Related articles