Automatiing mouse clicks
#1
Posted 09 September 2004 - 05:03 AM
#2
Posted 09 September 2004 - 10:33 AM
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.
#3
Posted 09 September 2004 - 11:07 AM
#4
Posted 09 September 2004 - 01:48 PM
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
Posted 09 September 2004 - 02:26 PM
#6
Posted 09 September 2004 - 02:56 PM
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.
#7
Posted 09 September 2004 - 03:43 PM
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
Posted 09 September 2004 - 04:24 PM
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
Posted 09 September 2004 - 05:21 PM
#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...
#10
Posted 09 September 2004 - 07:22 PM
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
Posted 09 September 2004 - 08:18 PM
#12
Posted 09 September 2004 - 09:49 PM
#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.
#13
Posted 09 September 2004 - 10:00 PM
#14
Posted 09 September 2004 - 10:17 PM
#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
Posted 09 September 2004 - 10:20 PM
#16
Posted 09 September 2004 - 10:22 PM
I guess it would have been easier...
hehehe - glad it works
#17
Posted 10 September 2004 - 12:16 AM



Help
Back to top








