I have an application I wrote that emulates clicking in Windows. The code below isn't a complete program, but should help you to simulate a click. I actually used a IdThreadComponent to create mine instead of the Timer, but the code I have would be too long for demonstration of this code.
The problem with your method relies on windows messaging system. The code below will create "actual mouse clicks" instead of simulated. The higher your priorityclass of your application the more reliable the clicking will be.
You might need to run your program "Run as Administrator", but not sure. If it doesn't work, try to see if running in Administrator helps?
You also might want to set your application to a higher priority.
Here is some of the code I extracted from a program I wrote for simulating clicks:
uses
Winapi.Windows
var
P: TPoint; // Global variable
procedure TForm1.SetPriorityLevel(P: byte);
begin
case p of
1: Setpriorityclass(GetCurrentProcess(), IDLE_PRIORITY_CLASS);
2: Setpriorityclass(GetCurrentProcess(), NORMAL_PRIORITY_CLASS);
3: Setpriorityclass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
4: Setpriorityclass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS);
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
eu: array [0..1] of TInput;
begin
SetCursorPos(P.X,P.Y);
ZeroMemory(@eu,sizeof(eu));
eu[0].Itype := INPUT_MOUSE;
eu[0].mi.dwFlags := MOUSEEVENTF_LEFTDOWN;
eu[1].Itype := INPUT_MOUSE;
eu[1].mi.dwFlags :=MOUSEEVENTF_LEFTUP;
if SendInput(2,eu[0],sizeof(TInput)) > 0 then
begin
inc(ClickCount);
end
else
begin
Label1.Caption := 'SendInput Failed..Sleeping';
sleep(50);
end;
end;
Please let me know if you have other questions.
More information about SendInput from here: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendinput and here: https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-mouseinput