problems in sending mouse clicks via SendInput API

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 a
needed by the SendInput. But the mouse clicks are always reported a
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


-
kumar_subrahmany
 
W

William DePalo [MVP VC++]

kumar_subrahmanya said:
I am facing problems in sending mouse clicks via SendInput API.

Mouse clicks are being sent but at the X,Y co-ordinates.
...
void SendMouseInput(unsigned int iX,unsigned int iY)
{
...
mousei.dwFlags=MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LEFTDOWN;

Don't you need to assert the MOUSEEVENTF_MOVE bit as well?

Regards,
Will
 
K

kumar_subrahmanya

Thanks a lot.
It works. In fact I used to send a seperate mouse move input before sending the mouse click. your solution is neater one. Thanks again for that.

But I thought it would work without the MOUSEEVENTF_MOVE flag being asserted!!!

Subra
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top