I need to be able to simulate a mouse click on a control in another process. I came up with the following method:
BOOL SimulateMouseClick(POINT* pPntAt)
{
//Simulate mouse left-click
//'pPntAt' = mouse coordinate on the screen
//RETURN:
// = TRUE if success
BOOL bRes = FALSE;
if(pPntAt)
{
//Get current mouse position
POINT pntMouse = {0};
BOOL bGotPntMouse = ::GetCursorPos(&pntMouse);
//Move mouse to a new position
::SetCursorPos(pPntAt->x, pPntAt->y);
//Send mouse click simulation
INPUT inp = {0};
inp.type = INPUT_MOUSE;
inp.mi.dx = pPntAt->x;
inp.mi.dy = pPntAt->y;
inp.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
if(SendInput(1, &inp, sizeof(inp)) == 1)
{
//Do I need to wait here?
Sleep(100);
inp.mi.dwFlags = MOUSEEVENTF_LEFTUP;
if(SendInput(1, &inp, sizeof(inp)) == 1)
{
//Do I need to wait here before restoring mouse pos?
Sleep(500);
//Done
bRes = TRUE;
}
}
//Restore mouse
if(bGotPntMouse)
{
::SetCursorPos(pntMouse.x, pntMouse.y);
}
}
return bRes;
}
My question is do I need to introduce those artificial delays like a human mouse click would have?