Sunday, 5 May 2019

Automatic mouse cursor move program

I needed a program to move the mouse cursor every so often to stop a PC from timing out while I was watching videos.

I didn't have admin rights for the PC so I could not adjust the timeout settings.


I use a TTimer to move the mouse cursor if input has been idle for a period of time and use a TTrackBar to set the length of the period of time.


The code to move the cursor uses the Windows API Mouse_Event() but doesn't actually move the cursor since it is set to move 0 pixels relative to the current cursor position.

unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls, Vcl.ComCtrls,
System.Actions, Vcl.ActnList, Vcl.StdActns;
type
TMouseMover = class(TForm)
Timer1: TTimer;
Label1: TLabel;
StaticText1: TStaticText;
Label2: TLabel;
TrackBar1: TTrackBar;
ActionList1: TActionList;
FileExit1: TFileExit;
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure TrackBar1Change(Sender: TObject);
private
{ Private declarations }
function GetIdleTime:Cardinal;
public
{ Public declarations }
end;
var
MouseMover: TMouseMover;
timelimit:cardinal;
implementation
{$R *.dfm}
procedure TMouseMover.FormCreate(Sender: TObject);
begin
label1.Caption:='';
{$IFDEF DEBUG}
timelimit:=trackbar1.Position*1000;
label2.Caption:=Format('Set to move mouse cursor after %u seconds idle time',[timelimit div 1000]);
{$ELSE}
timelimit:=trackbar1.Position*60*1000;
label2.Caption:=Format('Set to move mouse cursor after %u minutes idle time',[timelimit div 60000]);
{$ENDIF}
end;
function TMouseMover.GetIdleTime:Cardinal;
const
LastInputInfo = SizeOf(TLastInputInfo);
var
LastInput:TLastInputInfo;
begin
LastInput.cbSize:=LastInputInfo;
GetLastInputInfo(LastInput);
Result:=GetTickCount-LastInput.dwTime;
end;
procedure TMouseMover.Timer1Timer(Sender: TObject);
var
idletime:cardinal; // milliseconds
// mousePos:TPoint;
begin
idletime:=GetIdleTime;
label1.Caption:=Format('%.0n',[idletime*1.0]);//UIntToStr(idletime);
if idletime>timelimit then
begin
// GetCursorPos(mousePos);
// if mousePos.X < Screen.Width then SetCursorPos(mousePos.X+1,mousePos.Y) //doesn't reset LastInputInfo???
// else SetCursorPos(mousePos.X-1,mousePos.Y);
// Application.ProcessMessages;
Mouse_Event(MOUSEEVENTF_MOVE, 0, 0, 0, 0); // move relative to current position
Application.ProcessMessages;
// mouse_event(MOUSEEVENTF_LEFTDOWN,0, 0, 0, 0); //press left button
// mouse_event(MOUSEEVENTF_LEFTUP,0, 0, 0, 0);
end;
end;
procedure TMouseMover.TrackBar1Change(Sender: TObject);
begin
{$IFDEF DEBUG}
timelimit:=Trackbar1.Position*1000;
label2.Caption:=Format('Set to move mouse cursor after %u seconds idle time',[timelimit div 1000]);
{$ELSE}
timelimit:=Trackbar1.Position*60*1000;
label2.Caption:=Format('Set to move mouse cursor after %u minutes idle time',[timelimit div 60000]);
{$ENDIF}
end;
end.
view raw Unit1.pas hosted with ❤ by GitHub


No comments:

Post a Comment