I'm simulating mouse input on a WPF window using SendInput approach as suggested in this SO answer.
Basically what I do for MouseDown is:
//Some data initialization e.g. determine where to click
....
SetCursorPos(x, y);
var inputMouseDown = new INPUT { Type = 0 };
inputMouseDown.Data.Mouse.Flags = 0x0002; // left button down
var inputs = new[] { inputMouseDown };
SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
and immediately the same with:
//Prepare as before
....
var inputMouseUp = new INPUT { Type = 0 };
inputMouseUp.Data.Mouse.Flags = 0x0004; // left button up
//Send input as before
....
When I use this code to click a WPF button it works as expected but with a delay I cannot explain:
I measured the time from Mouse Up until a desired result for both this technique and actually clicking the control.
The Mouse Up detection was done using low-level mouse hooks.
In all my measurements it seems that WPF process faster (~200ms) physical mouse clicks then simulated ones.
I tried this hundreds of times with various tweaks (e.g. delay between down event and up event) but delta stayed the same.
What is the reason for this delay? how can this be resolved?