Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

My program does not maintain consistent mouse movements. using windows api #include Windows;

using the Windows.h header file. There are functions for manipulating the mouse. I don't fully understand how to read documentation yet but I was able to scrounge around for snippits of code and throw them together to suit my needs. The program does work but it doesn't stay consistently in place. I built this code with the intent of using it for the old classic flash game "Defend Your Castle". However since the mouse jumps around too much it doesn't work very well.

On a side note if anyone could point me in the right direction of building a dragable frame that I could drag around my desktop that would read the pixel data contained inside the frame. I would then use that information like if pixels containing this color run across this portion of the window then click it and drag it up and drop it.

This site is making me add a lot of comments so I am going on a bit of a tangent.

#include "pch.h"
#include <iostream>
#include <Windows.h>
#undef max
#include<limits>
#include "GLFW/glfw3.h"

using namespace std;
void LeftClickUp();
void LeftClickDown();
void MouseMoveDown();
void MouseMoveUp();
int main()
{
    system("color 2");
    int clicksPerSecond;
    cout << "Enter How fast should I click for you? (clicks per second, Max 100) ";
    while (!(cin >> clicksPerSecond) || clicksPerSecond <= 0 || clicksPerSecond > 100) {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "Invalid input.  Try again: ";
    }
    cout << "You enterd: " << clicksPerSecond << " clicks per second" << endl;
    cout << "Hold down the < 1 > key to start auto clicking" << endl;
    clicksPerSecond = 1000 / clicksPerSecond;
    while (1)
    {
        SHORT keyState = GetKeyState(0x31);
        bool isToggled = keyState & 1;
        bool isDown = keyState & 0x8000;
        while (isDown)
        {
            LeftClickDown();
            Sleep(5);
            MouseMoveUp();
            Sleep(5);
            LeftClickUp();
            Sleep(5);
            MouseMoveDown();
            Sleep(5);
            Sleep(clicksPerSecond);
            isDown = false;
        }
    }
}
void LeftClickDown()
{
    INPUT    Input = { 0 };
    // left down 
    Input.type = INPUT_MOUSE;
    Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
    Input.mi.time = 0;
    ::SendInput(1, &Input, sizeof(INPUT));
}
void MouseMoveUp()
{
    INPUT  Input = { 0 };
    Input.type = INPUT_MOUSE;
    Input.mi.dwFlags = MOUSEEVENTF_MOVE;
    Input.mi.dx = 0;
    Input.mi.time = 0;
    Input.mi.dy = -100;
    ::SendInput(1, &Input, sizeof(INPUT));
}
void MouseMoveDown()
{
    INPUT  Input = { 0 };
    Input.type = INPUT_MOUSE;
    Input.mi.dwFlags = MOUSEEVENTF_MOVE;
    Input.mi.dx = 0;
    Input.mi.time = 0;
    Input.mi.dy = 100;
    ::SendInput(1, &Input, sizeof(INPUT));
}
void LeftClickUp()
{
    INPUT    Input = { 0 };
    // left up
    ::ZeroMemory(&Input, sizeof(INPUT));
    Input.type = INPUT_MOUSE;
    Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
    Input.mi.time = 0;
    ::SendInput(1, &Input, sizeof(INPUT));
}

Comments