Hey, currently testing a autoclicker for a few games, I am quite new to C++
How can I use left mouse button to run a do statement for the mouse click function without it stopping the do statement
I've tried different flags including :
input.mi.dwFlags = (MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP);
with mouse_event(MOUSEEVENTF)
do {
if (GetAsyncKeyState(VK_LBUTTON) & 0x80000000) {
enabled = true;
while (GetAsyncKeyState(VK_LBUTTON) & 0x80000000) {
Sleep(1000 / cps);
INPUT input;
input.type = INPUT_MOUSE;
input.mi.dx = 0;
input.mi.dy = 0;
input.mi.dwFlags = (MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE | MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP);
input.mi.mouseData = 0;
input.mi.dwExtraInfo = NULL;
input.mi.time = 0;
SendInput(1, &input, sizeof(INPUT));
}
}
} while (true);
I am trying to make it so when you hold down left click, it runs the while statement containing the autoclicking function, without stopping the while statement due to repetitive clicking.
There is something you should notice in your code:
First, How to judge the return value of GetAsyncKeyState()
If the function succeeds, the return value specifies whether the key
was pressed since the last call to GetAsyncKeyState, and whether the
key is currently up or down. If the most significant bit is set, the
key is down, and if the least significant bit is set, the key was
pressed after the previous call to GetAsyncKeyState. However, you
should not rely on this last behavior; for more information, see the
Remarks.
the type of return value is SHORT which is 2 bytes. So try GetAsyncKeyState(VK_LBUTTON) & 0x8000 instead.
Second, How to use SendInput() to send different mouse state.
You should create a new INPUT data to save different action, but not just combine all of them.
Code Sample:
do {
if (GetAsyncKeyState(VK_LBUTTON) & 0x8000)
{
enabled = true;
while (GetAsyncKeyState(VK_LBUTTON) & 0x8000)
{
INPUT input[2];
input[0].type = input[1].type = INPUT_MOUSE;
input[0].mi.dx = 0;
input[0].mi.dy = 0;
input[0].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTDOWN;
input[1].mi.dx = 0;
input[1].mi.dy = 0;
input[1].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_LEFTUP;
SendInput(2, input, sizeof(INPUT) * 2);
}
}
} while (true);
Related
I am trying to understand how SendInput() works with mouse input. As an example, I have a dual monitor display. My 2160p monitor is "above" the 1440p primary monitor. I am trying to send mouse input to the bottom right corner of the above 2160 monitor. Unfortunately, the cursor just ends up in the bottom right corner of my 1440p primary monitor instead.
I know that the top left corner of my primary monitor is (0,0), and the bottom right is (2560,1440). From what I understand, all coordinates are in relation to the primary monitor. So shouldn't I just be able to subtract the resolution of the above monitor from the primary one to move to the exact same location on the above?
I have tried both adding and subtracting, but I keep ending up in the bottom corner of the primary monitor.
INPUT input;
input.type = INPUT_MOUSE;
input.mi.dx = (2560 - 3840) * (65536.f / (float)GetSystemMetrics(SM_CXSCREEN));
input.mi.dy = (1440 - 2160) * (65536.f / (float)GetSystemMetrics(SM_CYSCREEN));
input.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
input.mi.mouseData = 0;
input.mi.dwExtraInfo = NULL;
input.mi.time = 0;
SendInput(1, &input, sizeof(INPUT));
UPDATE: Here is one of my failed attempts using the RECT structure from GetMonitorInfo. I am probably making some sort of obvious mistake, but I can't figure it out.
DXGI_OUTPUT_DESC output_desc;
output->GetDesc(&output_desc);
MONITORINFOEX info;
info.cbSize = sizeof(info);
GetMonitorInfo(output_desc.Monitor, (LPMONITORINFO)&info);
INPUT input;
input.type = INPUT_MOUSE;
input.mi.dx = (info.rcMonitor.right) * (65536.f / (float)GetSystemMetrics(SM_CXVIRTUALSCREEN));
input.mi.dy = (info.rcMonitor.top) * (65536.f / (float)GetSystemMetrics(SM_CYVIRTUALSCREEN));
input.mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_VIRTUALDESK| MOUSEEVENTF_MOVE;
input.mi.mouseData = 0;
input.mi.dwExtraInfo = NULL;
input.mi.time = 0;
SendInput(1, &input, sizeof(INPUT));
void leftClick(){
INPUT input[2];
input[0].type = INPUT_MOUSE;
input[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
input[1].type = INPUT_MOUSE;
input[1].mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput(2, input, sizeof(INPUT));
cout<<"click down\n";
cout<<"click up\n\n";
/*
mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
*/
}
ReadProcessMemory(process, (void*)address, &value, sizeof(value), 0);
if (value == 1){
if (clock() - click_time >= 500){
cout<<"click time = "<< click_time <<endl;
cout<<clock() - click_time <<endl;
leftClick();
click_time = clock();
}
}
This gives an output of
click time = 0
57209
click down
click up
click time = 57222
501
click down
click up
click time = 57738
500
click down
click up
As you can see, the clicks should be happening, but this doesn't send any clicks. It only actually clicks if I take out all the clock stuff. Same thing happens if I use mouse_event instead of SendInput.
edit: I changed SendInput up there, didn't change any behavior. The commented mouse_events gives the same behavior as well
edit: It seems I can't make a minimal reproducible example. Without ReadProcessMemory, it works fine with a delay, it only doesn't send clicks with a delay while reading the process. Looking in to issues with combining ReadProcessMemory with any sort of delayed input.
The target process/window can detect SendInput and block it using the LLMHF_INJECTED flag.
If it's not blocking the input, you should SendInput() once with MOUSEEVENTF_LEFTDOWN, then after a delay, you should call it again with MOUSEEVENTF_LEFTUP. You don't know how the target process is processing input, adding a delay has been helpful for me in several situations.
Here's an example usint std::chrono for 1 delay timer, I used a sleep between DOWN & UP, in an effort to not over complicate the solution more than it needs to be for a simple example.
#include <chrono>
#include <thread>
int main()
{
INPUT input{ 0 };
input.type = INPUT_MOUSE;
bool bClick = false;
using Clock = std::chrono::steady_clock;
std::chrono::time_point<std::chrono::steady_clock> start, now;
std::chrono::milliseconds duration;
start = Clock::now();
while (true)
{
//toggles it on and off with one key
if (GetAsyncKeyState('U') & 1)
bClick = !bClick;
if (bClick)
{
now = Clock::now();
duration = std::chrono::duration_cast<std::chrono::milliseconds>(now - start);
if (duration.count() >= 100)
{
input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
SendInput(1, &input, sizeof(INPUT));
std::this_thread::sleep_for(std::chrono::milliseconds(30));
input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput(1, &input, sizeof(INPUT));
start = std::chrono::steady_clock::now();
}
}
}
return 0;
}
You can toggle it on and off with the U key.
I was dabbling with positioning of the mouse pointer which I had no trouble at all with, but trying to simulate a left click is giving me problems at the moment.
#include <Windows.h>
int main()
{
SetCursorPos(100, 450);
Sleep(3000);
SetCursorPos(1600, 450);
int n = 1;
while (n <= 10);
{
SetCursorPos(1600, 450);
LeftClick();
n++;
}
}
My LeftClick() function is as follows:
void LeftClick()
//down
INPUT Input = {0};
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
::SendInput(1, &Input, sizeof(INPUT));
//up
::ZeroMemory(&Input, sizeof(INPUT));
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
::SendInput(1, &Input, sizeof(INPUT));
I've looked around and so far everything I've seen hasn't worked for me.
Please give me any help with anything to do with my sloppy code, not just how I am trying to simulate clicking,
the issue I'm having is that upon executing this code, it clicks, and then my monitor turns blue and says "HDMI no cable connected", and either returns back to normal after 1 second, or stays on that screen until I perform an action (click, alt etc).
Anybody with any idea on why this is happening would be appreciated.
#include "stdafx.h"
#include <Windows.h>
using namespace std;
void function() {
INPUT Input;
Input.type = INPUT_MOUSE;
Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
SendInput(true, &Input, sizeof(Input));
Input.mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput(true, &Input, sizeof(Input));
};
int main()
{
Sleep(3000);
function();
return 0;
}
Also, I'm using Visual Studio 2017 Community. This problem occurs when debugging the code, and when running the executable. There are no errors or warnings with my code.
You are not initializing the INPUT structure properly thus (probably) invoking undefined behavior. Initialize the structure fields to zeros using:
INPUT Input = {};
You can also go with an array of two elements:
INPUT Input[2] = {0};
followed by a:
Input[0].type = INPUT_MOUSE;
Input[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
Input[1].type = INPUT_MOUSE;
Input[1].mi.dwFlags = MOUSEEVENTF_LEFTUP;
and one call to SendInput:
SendInput(2, Input, sizeof(INPUT));
You are not initializing most of INPUT fields sending some garbage and potentially triggering Undefined Behavior. You should at least fill them with zeros:
Input.type = INPUT_MOUSE;
Input.mi.dx = 0;
Input.mi.dy = 0;
Input.mi.mouseData = 0;
Input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
Input.mi.time = 0;
Input.mi.dwExtraInfo = 0;
I am using SendInput() to send relative positions of the mouse. First ill tel you what am doing.
i use my finger to move the mouse. So first i track my finger in a 640x480 image and get the absolute position in pixels with in the image.
then i send that position to the following method to generate relative mouse position commands using send input.
When the finger goes to the left boundary (xlim1) or the right boundary (xlim2) cursor keep scrolling horizontally to either left or right depending on which limit.
The issue is when i run the code and just when the cursor starts to move, screen goes to black.
when i comment the part else if(cx >= prevX && cx > xlim2){ .... } section, then it works.. (So the when finger point goes to right limit of the image it cursor keeps scrolling horizontally to the right. commented part enables the left horizontal scrolling).
bool first variable will be true if this is the first time, we capture the finger. Otherwise it is false.
void movMouse(int cx, int cy, bool first){
static int prevX = 0;
static int prevY = 0;
static int leftPrevX;
static int rightPrevX;
int mx,my;
if(first == true){
prevX = cx;
prevY = cy;
}
else{
mx = (cx - prevX);
my = (cy - prevY);
if(cx <= prevX && cx < xlim1){
mx = -20;
INPUT input;
input.type = INPUT_MOUSE;
input.mi.mouseData = 0;
input.mi.dx = -(mx);
input.mi.dy = (my);
input.mi.dwFlags = MOUSEEVENTF_MOVE;
SendInput(1, &input, sizeof(input));
}
else if(cx >= prevX && cx > xlim2){
mx = 20;
INPUT input;
input.type = INPUT_MOUSE;
input.mi.mouseData = 0;
input.mi.dx = -(mx);
input.mi.dy = (my);
input.mi.dwFlags = MOUSEEVENTF_MOVE;
SendInput(1, &input, sizeof(input));
}
else {
INPUT input;
input.type = INPUT_MOUSE;
input.mi.mouseData = 0;
input.mi.dx = -(mx);
input.mi.dy = (my);
input.mi.dwFlags = MOUSEEVENTF_MOVE;
SendInput(1, &input, sizeof(input));
}
prevX = cx;
prevY = cy;
}
}
Try
ZeroMemory(&input,sizeof(input));
also intialize all the variables including input.time
it worked for me :)
I ran into this same problem, even though I was calling ZeroMemory and doing everything else correctly. I was using input.mi.time to inform Windows of the spacing between the clicks, e.g. so double-click would work correctly. However I was getting the 'time' values from a remote computer. Because they differed from the local computer's time it caused Windows to invoke the screen saver! To workaround the problem I added some logic to detect the skew between the computers and bring the values somewhat in line with each other.
In summary: Make sure that input.mi.time is either zero or a value somewhat close to GetTickCount(). Using ZeroMemory to initialize the variable is an excellent suggestion.