Pause a while loop in motion.

I am very new to C++ and only know some basics, Me and my friend have fooled around and made a program that I actually find very useful to me. An Auto-clicking program, though I want to find out how to make it pause while its working by pressing a certain key (ESC for example) Because its bothersome while its clicking to try and click the exit key. Here is the code:


#include <iostream>
#include <windows.h>
#include <conio.h>
#include <stdio.h>
using namespace std;
int main(int argc, char *argv[])
{
SetConsoleTitle("Brian's and Michael's AutoClicker)");
int ch;
int n = 1;
int x;
int y;
int times;
int done;
double sleepy;
double time;
string choice;
start:
cout << "Brian's Auto Clicker: Improved" << endl;
cout << "Version 1.0\n\nPress Escape to exit whenever you want.";
cout << endl << endl << "How Many Times Would You Like To Click: ";
cin >> times;
cout << endl << "How Much Time Should I Wait Inbetween Clicks (MS): ";
cin >> sleepy;
cout << endl << "How many seconds until clicking start: ";
cin >> time;
done = 0;
cout << endl << "Starting in 5...";
Sleep(time*1000/5);
cout << endl << "Starting in 4...";
Sleep(time*1000/5);
cout << endl << "Starting in 3...";
Sleep(time*1000/5);
cout << endl << "Starting in 2...";
Sleep(time*1000/5);
cout << endl << "Starting in 1...";
Sleep(time*1000/5);
cout << endl << "STARTED";
while (done <= times)
{
Sleep(sleepy);
mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
done++;
}
cout << endl << endl << "Auto-Clicking Finished.";
cout << endl << endl << "(y/n) Again: ";
cin >> choice;
if (choice == "y")
{
system("cls");
goto start; }
cin.get();}

The main problem is once I start lets say, 10000 clicks in 10Millisecond increments, it goes to fast for me to control it to hit the X in the top right corner. Is there a function to monitor the ESC key so if its pressed it'll pause/exit program?
Just wanted to post it with code tags and indentation.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <stdio.h>
using namespace std;

int main(int argc, char *argv[])
{
  SetConsoleTitle("Brian's and Michael's AutoClicker)");
  int ch;
  int n = 1;
  int x;
  int y;
  int times;
  int done;
  double sleepy;
  double time;
  string choice;

  start:
  
  cout << "Brian's Auto Clicker: Improved" << endl;
  cout << "Version 1.0\n\nPress Escape to exit whenever you want.";
  cout << endl << endl << "How Many Times Would You Like To Click: ";
  cin >> times;
  cout << endl << "How Much Time Should I Wait Inbetween Clicks (MS): ";
  cin >> sleepy;
  cout << endl << "How many seconds until clicking start: ";
  cin >> time;
  done = 0;
  cout << endl << "Starting in 5...";
  Sleep(time*1000/5);
  cout << endl << "Starting in 4...";
  Sleep(time*1000/5);
  cout << endl << "Starting in 3...";
  Sleep(time*1000/5);
  cout << endl << "Starting in 2...";
  Sleep(time*1000/5);
  cout << endl << "Starting in 1...";
  Sleep(time*1000/5);
  cout << endl << "STARTED";
  
  while (done <= times)
  {
    Sleep(sleepy);
    mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
    done++;
  }
  
  cout << endl << endl << "Auto-Clicking Finished.";
  cout << endl << endl << "(y/n) Again: ";
  cin >> choice;
  
  if (choice == "y")
  {
    system("cls");
    goto start; 
  }
  cin.get();
}
Last edited on
I don't use CLR/CLI much and it's been several months since I read about event handling, but can't you place a check for a key_press event inside your while loop?

something like this;

while(done<=times && !(keyPressed=="ESCAPE"))// while keyPressed doesn't = "ESCAPE"...
{
...
}

I don't know the actual key words or anything, so you'd have to replace my keyPressed variable with the actual CLI expression that checks for a key being pressed, but I think CLR/CLI does the keyboard events in this kind of manner. This way the program would check for the escape key each time that the while loop runs and it shouldn't pause the program while checking.
Last edited on
I fixed it, I placed a

if(GetAsyncKeyState(VK_ESCAPE)){
    cout << "You pressed ESCAPE." << endl;
    break;
    }

in the while loop, That way when I press ESCAPE, it breaks the loop and still asks if I want to go again.
Just a note; your using output tags instead of code tags. Use the button that looks like this [<>] for code tags.
Last edited on
RosenShock - iseeplusplus is saying that when you post code here to use the "Source code" button instead of the "Program output" button. I don't care either way, but the "Source code" button does help with readability. (Sorry, I only mention because it took me a minute to understand what he was saying exactly, I thought he was talking about something with the in-line code)

Glad you were able to work the code out, congrats!

Topic archived. No new replies allowed.