Skip to content

Instantly share code, notes, and snippets.

@juaxix
Last active August 17, 2019 16:22
Show Gist options
  • Save juaxix/94dc280efb449800ba793e1e3569c07c to your computer and use it in GitHub Desktop.
Save juaxix/94dc280efb449800ba793e1e3569c07c to your computer and use it in GitHub Desktop.
Fake a mouse click in X,Y of the screen with loop and delay
#include <iostream>
#include <windows.h>
#include <string>
// LeftClick function
void LeftClick()
{
INPUT Input = { 0 };
// left down
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
::SendInput(1, &Input, sizeof(INPUT));
// left up
::ZeroMemory(&Input, sizeof(INPUT));
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
::SendInput(1, &Input, sizeof(INPUT));
}
// MouseMove function
void MouseMove(const int x, const int y)
{
const double fScreenWidth = ::GetSystemMetrics(SM_CXSCREEN) - 1;
const double fScreenHeight = ::GetSystemMetrics(SM_CYSCREEN) - 1;
const double fx = x * (65535.0f / fScreenWidth);
const double fy = y * (65535.0f / fScreenHeight);
INPUT Input = { 0 };
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
Input.mi.dx = static_cast<LONG>(fx);
Input.mi.dy = static_cast<LONG>(fy);
::SendInput(1, &Input, sizeof(INPUT));
}
int main(int argc, char** argv)
{
if(argc<4)
{
std::cout << "Not enough parameters, use:" << argv[0] << " x y loops delay" << std::endl;
return 1;
}
const int x = argc > 0 ? std::stoi(argv[1]) : 0;
const int y = argc > 1 ? std::stoi(argv[2]) : 0;
int loops = argc > 2 ? std::stoi(argv[3]) : -1; //infite loop
const auto delay = argc > 3 ? std::stoi(argv[4]) : 500; //milliseconds
while (true)
{
MouseMove(x, y);
LeftClick();
Sleep(delay);
if (loops > 0) loops--;
if (loops == 0)break;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment