Problems sending mouse inputs using SendInput

  • Thread starter Thread starter kumar_subrahmanya
  • Start date Start date
K

kumar_subrahmanya

Hi,
I am facing problems in sending mouse clicks via SendInput API.

Mouse clicks are being sent but at the X,Y co-ordinates.

I am mapping my monitor to the (0,0,65535,65535) virtual monitor as
needed by the SendInput. But the mouse clicks are always reported at
the "current cursor location" (I am testing it with the notepad)
irrespective of the X,Y co-ordinates specified.

Here is the code.


void SendMouseInput(unsigned int iX,unsigned int iY)
{
MOUSEINPUT mousei={0};
INPUT Input={0};
BlockInput(true);
::ZeroMemory(&Input,sizeof(INPUT));
::ZeroMemory(&mousei,sizeof(MOUSEINPUT));
mousei.dx=VirtualXFromAbsoluteX(iX);
mousei.dy=VirtualYFromAbsoluteY(iY);
mousei.dwFlags=MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTDOWN;
Input.type = INPUT_MOUSE;
Input.mi = mousei;

if(::SendInput(1,&Input,sizeof(Input))==0)
ShowError("SendMouseInput");


mousei.dwFlags=MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTUP;
Input.type = INPUT_MOUSE;
Input.mi = mousei;

if(::SendInput(1,&Input,sizeof(Input))==0)
ShowError("SendMouseInput");
BlockInput(false);
Sleep(1000);

}
static int VirtualXFromAbsoluteX ( int iX )
{

double Width = GetSystemMetrics ( SM_CXSCREEN ) ;
double Val = ( ((double)iX/Width) * (double)(65535) ) ;

double Ceil = ceil ( Val ) ;
double Floor = floor ( Val ) ;

if ( Ceil > Floor )
{
iX = (int)Ceil ;
}
else
{
iX = (int)Floor ;
}
return ( iX ) ;
}
static int VirtualYFromAbsoluteY ( int iY )
{

double Height = GetSystemMetrics ( SM_CYSCREEN ) ;
double Val = ( ((double)iY/Height) * (double)(65535) ) ;

double Ceil = ceil ( Val ) ;
double Floor = floor ( Val ) ;

if ( Ceil > Floor )
{
iY = (int)Ceil ;
}
else
{
iY = (int)Floor ;
}
return ( iY ) ;
}

void ShowError(LPTSTR lpszFunction)
{
TCHAR szBuf[80];
LPVOID lpMsgBuf;
DWORD dw = GetLastError();

FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dw,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );

wsprintf(szBuf,
"%s failed with error %d: %s",
lpszFunction, dw, lpMsgBuf);

::MessageBox(NULL, szBuf, "Error", MB_OK);

LocalFree(lpMsgBuf);
}

Am I doing something wrong?

PS: I am able to send key board events using SendInput.

Thanks a load in advance.
 
Hi,

Call SetCursorPos() before calling SendInput().

Hope this helps.

Dinesh Venugopalan
 
Back
Top