By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,156 Members | 989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,156 IT Pros & Developers. It's quick & easy.

problems in sending mouse clicks via SendInput API

P: n/a

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_LE FTDOWN;
Input.type = INPUT_MOUSE;
Input.mi = mousei;

if(::SendInput(1,&Input,sizeof(Input))==0)
ShowError("SendMouseInput");
mousei.dwFlags=MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_LE FTUP;
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_subrahmanya
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

May 31 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"kumar_subrahmanya" <ku**********************@mail.codecomments.com> wrote
in message news:ku**********************@mail.codecomments.co m...
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_LE FTDOWN;


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

Regards,
Will
May 31 '06 #2

P: n/a
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
Jul 4 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.