Timed If statement

I'm currently working on a bot that is in a loop for clicking. Everything's working perfectly although there's one problem... I can't stop it constantly clicking without closing the program.

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
#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
    cout<<"Instructions:\n";
    cout<<" -Move your mouse over where you want it.\n";
    cout<<" -Hit '1'.\n";
    cout<<" -And then hit that enter button.\n";
    cout<<"Be careful. It constantly clicks.";

    int mousePos;
    cin>>mousePos;
    
    unsigned int count;    //For huge number
    for (int i = 0; i < count; ++i)
    {
        mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

        Sleep(500);    //Wait 500 miliseconds
    }

    cin.get();
    return 0;
}


Is it possible nest an If statement in there that's timed? Maybe something like this:

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
#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
    cout<<"Instructions:\n";
    cout<<" -Move your mouse over where you want it.\n";
    cout<<" -Hit '1'.\n";
    cout<<" -And then hit that enter button.\n";
    cout<<"Be careful. It constantly clicks.";

    int mousePos;
    cin>>mousePos;
    
    unsigned int count;    //For huge number
    for (int i = 0; i < count; ++i)
    {
        mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

        if(you press something within 500 milisecs)
        {
            close the program;
        }
        else
        {
            continue the loop;
        }
    }

    cin.get();
    return 0;
}


Yeah... Is it possible to have a timed if? thanks
Last edited on
Just put a if ( GetAsyncKeyState(VK_ESCAPE) >> 15 ) break; statement
inside your for loop. Then, you can stop the loop by holding the escape key.

Useful link -> http://msdn.microsoft.com/en-us/library/ms646293(v=vs.85).aspx
Last edited on
YESS! Works, Thanks!

Also checked out that link
Question is: what value does count hold? I don't see an assignment.
I didn't assign it because I wanted it to be infinity, but since you can't do that in C++, just made it an unimaginably large number.
Then this:
1
2
3
4
for (int i = 0; i < count; ++i)
{
    // ...
}

May be better expressed as this:
1
2
3
4
while(true) // loop forever
{
    // ...
}

Last edited on

unsigned int count; //For huge number
for (int i = 0; i < count; ++i)


This is undefined behaviour, you know?
Woah yeah, just changed it to a while(1) loop. No idea why I didn't think of that before, and to rapid... Maybe ;)
Topic archived. No new replies allowed.