Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account



Photo

Automatiing mouse clicks

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
16 replies to this topic

#1
jjj93421

jjj93421

    Newbie

  • Member
  • 17 posts
  • Joined 24-August 04
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.


How to remove advertisement from MSFN

#2
HeartsOfWar

HeartsOfWar

    Newbie

  • Member
  • 49 posts
  • Joined 25-February 04
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.
Posted Image

#3
SiMoNsAyS

SiMoNsAyS

    C'mon you apes! You wanna live forever!?

  • Patrons
  • 3,414 posts
  • Joined 17-May 04
i'll recommend an AutoIt script that i think it's easier. check some AutoIt examples

#4
jjj93421

jjj93421

    Newbie

  • Member
  • 17 posts
  • Joined 24-August 04
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.

#5
jjj93421

jjj93421

    Newbie

  • Member
  • 17 posts
  • Joined 24-August 04
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 ?

#6
HeartsOfWar

HeartsOfWar

    Newbie

  • Member
  • 49 posts
  • Joined 25-February 04
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.
Posted Image

#7
jjj93421

jjj93421

    Newbie

  • Member
  • 17 posts
  • Joined 24-August 04
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;
}


#8
jjj93421

jjj93421

    Newbie

  • Member
  • 17 posts
  • Joined 24-August 04
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.

#9
HeartsOfWar

HeartsOfWar

    Newbie

  • Member
  • 49 posts
  • Joined 25-February 04
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...
Posted Image

#10
jjj93421

jjj93421

    Newbie

  • Member
  • 17 posts
  • Joined 24-August 04
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.

#11
jjj93421

jjj93421

    Newbie

  • Member
  • 17 posts
  • Joined 24-August 04
It says XBUTTON1 "undeclared identifier" . ****, well, I guess I'll try to figure it out tomorrow.

#12
HeartsOfWar

HeartsOfWar

    Newbie

  • Member
  • 49 posts
  • Joined 25-February 04
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.
Posted Image

#13
jjj93421

jjj93421

    Newbie

  • Member
  • 17 posts
  • Joined 24-August 04
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 ?

#14
jjj93421

jjj93421

    Newbie

  • Member
  • 17 posts
  • Joined 24-August 04
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.

#15
HeartsOfWar

HeartsOfWar

    Newbie

  • Member
  • 49 posts
  • Joined 25-February 04
Use SetFocus function

see here - http://msdn.microsof...ns/setfocus.asp
Posted Image

#16
HeartsOfWar

HeartsOfWar

    Newbie

  • Member
  • 49 posts
  • Joined 25-February 04
Yeah, I was going to tell you to use Mouse_event, but I decided against it because it isn't the most up-to-date...

I guess it would have been easier...

hehehe - glad it works
Posted Image

#17
jjj93421

jjj93421

    Newbie

  • Member
  • 17 posts
  • Joined 24-August 04
To get that SendInput .cpp file to compile I had to change the windows.h file to 0x0501 from 0x0400 and also change stdafx.h. Are there any consequences from this later? I'm not a software developer so I wouldn't think that it is important to me.