Jump to content

Automatiing mouse clicks


Recommended Posts

I want to be able to automate a mouse click so that when I start up a program it will click on a button in a window automatically. I already have a handle to this window . I know the x and y coords. Can anyone help a beginning programmer out? I am using win32 and it is a console program. Also, this button won't move and the window is always in the same spot on my desktop, therefore the x and y coords will always be the same if this helps anyone out. I want to do it the easiest way possible. I know how to program with c++ but not that well and my windows programming is sketchy. If someone can get me started, a little experimenting on my part should solve what I need.

Link to comment
Share on other sites


Use the GetCursorPos function, documentation found here, to retrieve the mouse position.

Use the SetCursorPos function, documentation found here, to reposition the mouse.

Use the SendInput function, documentation found here. You will need to reference MOUSEINPUTS, documentation found here, to simulate LEFTDOWN, LEFTUP, etc... events.

-----------------------------------------------------------------------------------------------

Keep in mind that the window may appear at the same X, Y coordinates on your machine, but other people use different resolutions and a different resolution will change the X, Y coordinates.

Since you have the handle to the window or can easily retrieve the handle, you may want to retrieve the X, Y coordinates before simulating the mouse to be safe.

Link to comment
Share on other sites

Ok, appreciate the advice Hearts and Simon. What I don't understand is the SendInput function. I don't understand the parameters.

Parameters

nInputs

[in] Specifies the number of structures in the pInputs array.

pInputs

[in] Pointer to an array of INPUT structures. Each structure represents an event to be inserted into the keyboard or mouse input stream.

cbSize

[in] Specifies the size, in bytes, of an INPUT structure. If cbSize is not the size of an INPUT structure, the function will fail.

************************

Number of structures in the inputs array ?? Huh ? Pointer to an array of input structures ? What does arrays have to do with this ? Ok the size of it but how do I find these things out ? Please help. The rest looks easy.

Link to comment
Share on other sites

Ok. I got the handle of the button. I got the caption of the button and I have the class of it. I didn't even know these things existed until 5 minutes ago. I used Spy++. Anyone have ideas of where to go from here? Is this what I put into SendInput ?

Link to comment
Share on other sites

SendInput is the latest function that Win32 API uses... SendInput integrates the ability of sending a command to the keybord, mouse, or other hardware that can support inputs.

Since SendInput is so versatile, you must first set-up some initial data...

first create a data structure known as INPUT

INPUT test;

You want to initiate a mouse event, so we need to focus on an INPUT structure that is suited for a mouse. This leads us to INPUT_MOUSE. See this document for more information or further explanation of the MOUSE_INPUT structure.

Load the structure accordingly...

test.type = INPUT_MOUSE;
test.mi.dx = 25;
test.mi.dy = 25;
test.mi.mouseData = XBUTTON1;
test.mi.dwflags = MOUSEEVENTF_LEFTDOWN;
test.mi.time = 0;
test.mi.dwExtraInfo = 0;

Now create a pointer of type INPUT to the above variable...

INPUT *InpPtr = test;

Once that is done, now call SendInput passing the parameters...

SendInput(1,InpPtr,sizeof(INPUT));

The program should send a left mouse DOWN message to x coordinate 25 and y coordinate 25.

I think you will also need to simulate a left mouse UP message to the same coordinates in order to qualify as a full click.

Link to comment
Share on other sites

Hey, Hearts, I'm back. I got 3 errors when I tried to compile it. Also, do I need to use MOUSEINPUT , or is that just FYI . Sorry for so many questions. This is what it said:

Compiling...

mouse click 3.cpp

C:\Program Files\Microsoft Visual Studio\MyProjects\mouse click 3\mouse click 3.cpp(65) : error C2146: syntax error : missing ';' before identifier 'test'

C:\Program Files\Microsoft Visual Studio\MyProjects\mouse click 3\mouse click 3.cpp(65) : error C2501: 'INPUT' : missing storage-class or type specifiers

C:\Program Files\Microsoft Visual Studio\MyProjects\mouse click 3\mouse click 3.cpp(65) : fatal error C1004: unexpected end of file found

Error executing cl.exe.

mouse click 3.obj - 3 error(s), 0 warning(s)

Do I not have an #include that I need ? Here is the code:

#include <windows.h>
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>


int a,b;

int xstart=-10;
int ystart=-20;

char dd;

char * pch;
HWND hwnd;
char texttitle[255]={0};

char word[255];

BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
{

GetWindowText(hWnd, texttitle ,sizeof(texttitle));
if(texttitle[0] != 0){
pch=strstr (texttitle,word);
if (pch!=NULL){
 hwnd=hWnd;}}


return TRUE;
}



INPUT test;

test.type = INPUT_MOUSE;
test.mi.dx = 380;
test.mi.dy = 457;
test.mi.mouseData = XBUTTON1;
test.mi.dwflags = MOUSEEVENTF_LEFTDOWN;
test.mi.time = 0;
test.mi.dwExtraInfo = 0;


INPUT *InpPtr = test;


int main(){


loop201:
cout<<"Input a word from window title \n";
cin.getline(word,255);


EnumWindows(EnumWindowsProc,0);



SetWindowPos( hwnd,
   HWND_TOP,
   xstart,
   ystart,
   2,
   2,
   SWP_NOSIZE
);


cout<<"Hit enter to automatically use mouse ";
cin.get();
cin.get();


SendInput(1,InpPtr,sizeof(INPUT));

return 0;
}

Link to comment
Share on other sites

Ok, figured it out except for the unexpect end of file found. I'm looking and looking and see no missed brackets ? I'm confused here.

Evidently, you need this in the file, too

#if (_WIN32_WINNT > 0x0400)

I found that info on another site. I would not have been able to come up with obviously since I'm new to this stuff. But what's up with the unexpected end of file found.

Link to comment
Share on other sites

Add this to the top

#define _WIN32_WINNT 0x0501

#define WINVER 0x0501

Remove what you said you needed... before

Also, change the code accordingly,

INPUT *InpPtr = new INPUT;

InpPtr->type = INPUT_MOUSE;
InpPtr->mi.dx = 380;
InpPtr->mi.dy = 457;
InpPtr->mi.mouseData = XBUTTON1;
InpPtr->mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
InpPtr->mi.time = 0;
InpPtr->mi.dwExtraInfo = 0;

Before you exit, make sure to delete *InpPtr

delete InpPtr;

This should work...

Link to comment
Share on other sites

Crap. It didn't like that. Here is the code now if you need it. It said this:

Compiling...

mouse 5.cpp

C:\Program Files\Microsoft Visual Studio\MyProjects\mouse 5\mouse 5.cpp(11) : warning C4005: 'WINVER' : macro redefinition

c:\program files\microsoft visual studio\myprojects\mouse 5\mouse 5.cpp(0) : see previous definition of 'WINVER'

C:\Program Files\Microsoft Visual Studio\MyProjects\mouse 5\mouse 5.cpp(49) : error C2143: syntax error : missing ';' before '->'

C:\Program Files\Microsoft Visual Studio\MyProjects\mouse 5\mouse 5.cpp(49) : error C2501: 'InpPtr' : missing storage-class or type specifiers

C:\Program Files\Microsoft Visual Studio\MyProjects\mouse 5\mouse 5.cpp(49) : error C2040: 'InpPtr' : 'int' differs in levels of indirection from 'struct tagINPUT *'

C:\Program Files\Microsoft Visual Studio\MyProjects\mouse 5\mouse 5.cpp(49) : error C2143: syntax error : missing ';' before '->'

C:\Program Files\Microsoft Visual Studio\MyProjects\mouse 5\mouse 5.cpp(50) : error C2143: syntax error : missing ';' before '->'

C:\Program Files\Microsoft Visual Studio\MyProjects\mouse 5\mouse 5.cpp(50) : error C2501: 'InpPtr' : missing storage-class or type specifiers

C:\Program Files\Microsoft Visual Studio\MyProjects\mouse 5\mouse 5.cpp(50) : error C2040: 'InpPtr' : 'int' differs in levels of indirection from 'struct tagINPUT *'

C:\Program Files\Microsoft Visual Studio\MyProjects\mouse 5\mouse 5.cpp(50) : error C2143: syntax error : missing ';' before '->'

C:\Program Files\Microsoft Visual Studio\MyProjects\mouse 5\mouse 5.cpp(51) : error C2143: syntax error : missing ';' before '->'

C:\Program Files\Microsoft Visual Studio\MyProjects\mouse 5\mouse 5.cpp(51) : error C2501: 'InpPtr' : missing storage-class or type specifiers

mouse 5.obj - 56 error(s), 1 warning(s)

#include <windows.h>
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>
#include <WinUser.h>
#include <stdafx.h>


#define _WIN32_WINNT 0x0501

#define WINVER 0x0501





int a,b;

int xstart=-10;
int ystart=-20;

char dd;

char * pch;
HWND hwnd;
char texttitle[255]={0};

char word[255];



BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
{

GetWindowText(hWnd, texttitle ,sizeof(texttitle));
if(texttitle[0] != 0){
pch=strstr (texttitle,word);
if (pch!=NULL){
 hwnd=hWnd;}}


return TRUE;
}



INPUT *InpPtr = new INPUT;

InpPtr->type = INPUT_MOUSE;
InpPtr->mi.dx = 380;
InpPtr->mi.dy = 457;
InpPtr->mi.mouseData = XBUTTON1;
InpPtr->mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
InpPtr->mi.time = 0;
InpPtr->mi.dwExtraInfo = 0;


INPUT *InpPtr2 = new INPUT;

InpPtr2->type = INPUT_MOUSE;
InpPtr2->mi.dx = 380;
InpPtr2->mi.dy = 457;
InpPtr2->mi.mouseData = XBUTTON2;
InpPtr2->mi.dwFlags = MOUSEEVENTF_LEFTUP;
InpPtr2->mi.time = 0;
InpPtr2->mi.dwExtraInfo = 0;




int main(){




loop201:
cout<<"Input a word from window title \n";
cin.getline(word,255);




EnumWindows(EnumWindowsProc,0);



SetWindowPos( hwnd,
   HWND_TOP,
   xstart,
   ystart,
   2,
   2,
   SWP_NOSIZE
);





cout<<"Hit enter to automatically use mouse ";
cin.get();
cin.get();


SendInput(1,InpPtr,sizeof(INPUT));
SendInput(1,InpPtr2,sizeof(INPUT));

delete InpPtr;



return 0;



}

I also tried an #endif at the end of my code with the previous way but it didn't like that either.

Link to comment
Share on other sites

I have edited your code...

#define _WIN32_WINNT 0x0501

#define WINVER 0x0501

#include <windows.h>
#include <iostream.h>
#include <WinUser.h>

int a,b;

int xstart=-10;
int ystart=-20;

char dd;

char * pch;
HWND hwnd;
char texttitle[255]={0};

char word[255];



BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam)
{

GetWindowText(hWnd, texttitle ,sizeof(texttitle));
if(texttitle[0] != 0){
pch=strstr (texttitle,word);
if (pch!=NULL){
hwnd=hWnd;}}


return TRUE;
}


int main(){

INPUT *InpPtr = new INPUT;

InpPtr->type = INPUT_MOUSE;
InpPtr->mi.dx = 380;
InpPtr->mi.dy = 457;
InpPtr->mi.mouseData = XBUTTON1;
InpPtr->mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
InpPtr->mi.time = 0;
InpPtr->mi.dwExtraInfo = 0;


INPUT *InpPtr2 = new INPUT;

InpPtr2->type = INPUT_MOUSE;
InpPtr2->mi.dx = 380;
InpPtr2->mi.dy = 457;
InpPtr2->mi.mouseData = XBUTTON2;
InpPtr2->mi.dwFlags = MOUSEEVENTF_LEFTUP;
InpPtr2->mi.time = 0;
InpPtr2->mi.dwExtraInfo = 0;

cout<<"Input a word from window title \n";
cin.getline(word,255);




EnumWindows(EnumWindowsProc,0);



SetWindowPos( hwnd,
  HWND_TOP,
  xstart,
  ystart,
  2,
  2,
  SWP_NOSIZE
);





cout<<"Hit enter to automatically use mouse ";
cin.get();
cin.get();


SendInput(1,InpPtr,sizeof(INPUT));
SendInput(1,InpPtr2,sizeof(INPUT));

delete InpPtr;



return 0;



}

Copy and paste what I have... should work fine...

You need to put the #defines at the very top, before the #includes...

Also, I don't see why you need stdafx or the other header file, so I removed them.

I also put your INPUT variable declarations within MAIN...

GLOBAL variables are very almost as bad as GOTO statements... you should try and keep them to a minimal if none at all.

Link to comment
Share on other sites

Yes yes, thanks Hearts. It compiles now. But, it isn't working. I'm investigating right now why it isn't. I am testing it on a poker game that I have where the button is at the bottom and it will deal your hand if you click on it and it isn't dealing. I think it may have something to do with bringing the app into the foreground or making it have focus ?

Link to comment
Share on other sites

Ok, I got it !!!! But, I have some bad news. This is the final code that works.

#include <windows.h>
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>


int main(){

cout<<"hit enter to click button";
cin.get();
cin.get();


SetCursorPos(404, 459);

mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);


return 0;

}

sorry man. I couldn't ever get SendInput to work. I thought I should tell you in case you ever need to do the same thing. You the man, though. Later.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...