Reputation: 11190
I am looking to create an overlay to a strategy game called Dune 2000. It is annoying that to create 10 soldiers, you have to click the icon everytime one is completed. There is no queue. So without interfering in how the game works, i would like to listen to the mouse movements, and when a click at position XY is made, i would like that to repeat for example ten times, with the right time inbetween. Is there any libary that allows me to do that?
Upvotes: 0
Views: 1139
Reputation: 8587
Below is a Autoclicker code for the right mousebutton. For the left mouse button use
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
and mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
.
#include <windows.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
bool KeyIsPressed( unsigned char k )
{
USHORT status = GetAsyncKeyState( k );
return (( ( status & 0x8000 ) >> 15 ) == 1) || (( status & 1 ) == 1);
}
int WINAPI WinMain( HINSTANCE hInst, HINSTANCE P, LPSTR CMD, int nShowCmd )
{
MessageBox( NULL, "[CTRL] + [SHIFT] + [HOME]: Start/Pause\n [CTRL] + [SHIFT] + [END]: Quit", "Instructions", NULL );
HWND target = GetForegroundWindow();
POINT pt;
RECT wRect;
int delay;
bool paused = true;
srand( time(NULL) );
while ( 1 )
{
if ( KeyIsPressed( VK_CONTROL ) && KeyIsPressed( VK_SHIFT ) && KeyIsPressed( VK_HOME ) )
{
paused = !paused;
if ( paused )
{
MessageBox( NULL, "Paused.", "Notification", NULL );
}
else
{
cout << "Unpaused.\n";
target = GetForegroundWindow();
cout << "Target window set.\n";
}
Sleep( 1000 );
}
// Shutdown.
if ( KeyIsPressed( VK_CONTROL ) && KeyIsPressed( VK_SHIFT ) && KeyIsPressed( VK_END ) )
{
MessageBox( NULL, "AutoClicker Shutdown.", "Notification", NULL );
break;
}
if ( paused == false && GetForegroundWindow() == target )
{
GetCursorPos( &pt );
GetWindowRect( target, &wRect );
// Make sure we are inside the target window.
if ( pt.x > wRect.left && pt.x < wRect.right && pt.y > wRect.top && pt.y < wRect.bottom )
{
mouse_event( MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0 );
mouse_event( MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0 );
}
delay = (rand() % 3 + 1) * 100;
Sleep( delay );
}
}
return 0;
}
Upvotes: 2