I want my code to keep on running without exiting. Is there a way to do this, or do I have to use an infinite loop to keep it running.
#include <iostream>
#include <windows.h>
#include <chrono>
#include <thread>
#include <math.h>
#include <random>
#include <fstream>
using namespace std;
int main()
{
POINT coord;
GetCursorPos(&coord);
fstream file;
file.open("example.txt", ios::out | ios::in);
// Reding from file
//closing the file
int xCoord;
int yCoord;
GetCursorPos(&coord);
if (MOUSEEVENTF_LEFTDOWN) {
cout << "Coords: " << coord.x << " , " << coord.y << "\n";
xCoord = coord.x;
yCoord = coord.y;
file << xCoord << "," << yCoord << endl;
}
return 0;
}
Basically, the idea is to log every left click a user has done and put them in a text file, but it just ends after getting one iteration. Is there a way for it to take more than 1 iteration? I've tried using a for infinite loop, but it just floods the whole file with the same coordinates.
I would use while(!GetAsyncKeystate(VK_END))
to keep the code running however for this to work as intended you want to also change if(MOUSEEVENTF_LEFTDOWN)
to if (GetAsyncKeyState(VK_LBUTTON )) {
So your code would now look like
int xCoord;
int yCoord;
while (!GetAsyncKeyState(VK_END)) {
GetCursorPos(&coord);
if (GetAsyncKeyState(VK_LBUTTON ) & 1) {
cout << "Coords: " << coord.x << " , " << coord.y << "\n";
xCoord = coord.x;
yCoord = coord.y;
file << xCoord << "," << yCoord << endl;
}
}
}
When you press the END key on your keyboard your program will stop:3
The problem here is that MOUSEEVENTF_LEFTDOWN
is a constant value. This documentation says the value is 0x0002
.
This means that:
if (MOUSEEVENTF_LEFTDOWN) // this is always true.
So if you add a loop this if condition is always true so it will record a value every iteration of the loop. You need to find a function that returns the state of the mouse button.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.