Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Start/Stop function using getch
Index -> Programming, C++ -> C++ Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Cancer Sol




PostPosted: Tue Apr 02, 2013 10:55 am   Post subject: Start/Stop function using getch

I'm not sure if I shouldn't use conio.h, but I did so I could use the getch function.
Basically, this is a part of my auto-clicker, and if the user pressed three, it'll start the clicking, and if they press three again, it'll stop clicking.

c++:

case 51://If the user pressed 3
            {
                while (1)
                {
                    char stop = getch();//I tried to make auto-clicker stop if 3 was pressed, but it won't really work all the time
                    while (stop != 51)//If it's not three, then it'll continue clicking
                    {
                        for (int i=amount; i>0; i--)//I'm going to change this, instead of having the auto-clicker clicking from the last coordinates to the first, it'll be first to last
                        {

                            SetCursorPos(x[i],y[i]);//Sets the cursor to the coordinates the user recorded from a different part of the program
                            Sleep(delay);//delay is on 0 by default, I gave it the value of 0 earlier
                            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
                        }
                    }
                }
           } 

Does anyone know how to make it stop if I press three, and make it pause even if the console window isn't highlighted?
Sponsor
Sponsor
Sponsor
sponsor
nullptr




PostPosted: Tue Apr 02, 2013 3:48 pm   Post subject: Re: Start/Stop function using getch

Cancer Sol @ Tue Apr 02, 2013 10:55 am wrote:
I'm not sure if I shouldn't use conio.h, but I did so I could use the getch function.
Basically, this is a part of my auto-clicker, and if the user pressed three, it'll start the clicking, and if they press three again, it'll stop clicking.

c++:

case 51://If the user pressed 3
            {
                while (1)
                {
                    char stop = getch();//I tried to make auto-clicker stop if 3 was pressed, but it won't really work all the time
                    while (stop != 51)//If it's not three, then it'll continue clicking
                    {
                        for (int i=amount; i>0; i--)//I'm going to change this, instead of having the auto-clicker clicking from the last coordinates to the first, it'll be first to last
                        {

                            SetCursorPos(x[i],y[i]);//Sets the cursor to the coordinates the user recorded from a different part of the program
                            Sleep(delay);//delay is on 0 by default, I gave it the value of 0 earlier
                            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
                        }
                    }
                }
           } 

Does anyone know how to make it stop if I press three, and make it pause even if the console window isn't highlighted?


As far as I know, the only way to get what you want is to use threads. See http://en.cppreference.com/w/cpp/thread/thread -- you'll need an up-to-date compiler like gcc 4.7. Otherwise, you'd need to wait for user input between each mouse click.
Cancer Sol




PostPosted: Tue Apr 02, 2013 5:11 pm   Post subject: Re: Start/Stop function using getch

nullptr @ 4/2/2013, 3:48 pm wrote:
Cancer Sol @ Tue Apr 02, 2013 10:55 am wrote:
I'm not sure if I shouldn't use conio.h, but I did so I could use the getch function.
Basically, this is a part of my auto-clicker, and if the user pressed three, it'll start the clicking, and if they press three again, it'll stop clicking.

c++:

case 51://If the user pressed 3
            {
                while (1)
                {
                    char stop = getch();//I tried to make auto-clicker stop if 3 was pressed, but it won't really work all the time
                    while (stop != 51)//If it's not three, then it'll continue clicking
                    {
                        for (int i=amount; i>0; i--)//I'm going to change this, instead of having the auto-clicker clicking from the last coordinates to the first, it'll be first to last
                        {

                            SetCursorPos(x[i],y[i]);//Sets the cursor to the coordinates the user recorded from a different part of the program
                            Sleep(delay);//delay is on 0 by default, I gave it the value of 0 earlier
                            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
                        }
                    }
                }
           } 

Does anyone know how to make it stop if I press three, and make it pause even if the console window isn't highlighted?


As far as I know, the only way to get what you want is to use threads. See http://en.cppreference.com/w/cpp/thread/thread -- you'll need an up-to-date compiler like gcc 4.7. Otherwise, you'd need to wait for user input between each mouse click.

Will the GNU-GCC compiler that's included with Code Blocks MingW recognize it?
Insectoid




PostPosted: Tue Apr 02, 2013 7:16 pm   Post subject: RE:Start/Stop function using getch

It's a library. You install it and include it and it will work. On Windows. Some libraries only work on a specific operating system or architecture. conio.h is one of them.
nullptr




PostPosted: Tue Apr 02, 2013 9:50 pm   Post subject: Re: Start/Stop function using getch

Cancer Sol @ Tue Apr 02, 2013 5:11 pm wrote:
nullptr @ 4/2/2013, 3:48 pm wrote:
Cancer Sol @ Tue Apr 02, 2013 10:55 am wrote:

. . .


As far as I know, the only way to get what you want is to use threads. See http://en.cppreference.com/w/cpp/thread/thread -- you'll need an up-to-date compiler like gcc 4.7. Otherwise, you'd need to wait for user input between each mouse click.

Will the GNU-GCC compiler that's included with Code Blocks MingW recognize it?


I'm not sure what version is included. Try this code and see if it works:

c++:

#include <thread>
#include <iostream>

using namespace std;

void test()
{
    cout << "Thread 2" << endl;
}

int main()
{
    thread t(test);
    cout << "Thread 1" << endl;
    t.join();

    return 0;
}


Insectoid wrote:

It's a library. You install it and include it and it will work. On Windows. Some libraries only work on a specific operating system or architecture. conio.h is one of them.


Isn't thread platform-independent and installed by default, as part of the STL? Or were you talking specifically about conio.h?
Cancer Sol




PostPosted: Wed Apr 03, 2013 1:06 pm   Post subject: Re: Start/Stop function using getch

nullptr @ 4/2/2013, 9:50 pm wrote:
Cancer Sol @ Tue Apr 02, 2013 5:11 pm wrote:
nullptr @ 4/2/2013, 3:48 pm wrote:
Cancer Sol @ Tue Apr 02, 2013 10:55 am wrote:

. . .


As far as I know, the only way to get what you want is to use threads. See http://en.cppreference.com/w/cpp/thread/thread -- you'll need an up-to-date compiler like gcc 4.7. Otherwise, you'd need to wait for user input between each mouse click.

Will the GNU-GCC compiler that's included with Code Blocks MingW recognize it?


I'm not sure what version is included. Try this code and see if it works:

c++:

#include <thread>
#include <iostream>

using namespace std;

void test()
{
    cout << "Thread 2" << endl;
}

int main()
{
    thread t(test);
    cout << "Thread 1" << endl;
    t.join();

    return 0;
}


Insectoid wrote:

It's a library. You install it and include it and it will work. On Windows. Some libraries only work on a specific operating system or architecture. conio.h is one of them.


Isn't thread platform-independent and installed by default, as part of the STL? Or were you talking specifically about conio.h?

Alright, I'll try it right after, but what does join for t.join do?
Edit: Nope, won't work.
code:
#error This file requires compiler and library support for the \
ISO C++ 2011 standard. This support is currently experimental, and must be \
enabled with the -std=c++11 or -std=gnu++11 compiler options.
nullptr




PostPosted: Wed Apr 03, 2013 5:08 pm   Post subject: Re: Start/Stop function using getch

Cancer Sol @ Wed Apr 03, 2013 1:06 pm wrote:
nullptr @ 4/2/2013, 9:50 pm wrote:
Cancer Sol @ Tue Apr 02, 2013 5:11 pm wrote:
nullptr @ 4/2/2013, 3:48 pm wrote:
Cancer Sol @ Tue Apr 02, 2013 10:55 am wrote:

. . .


As far as I know, the only way to get what you want is to use threads. See http://en.cppreference.com/w/cpp/thread/thread -- you'll need an up-to-date compiler like gcc 4.7. Otherwise, you'd need to wait for user input between each mouse click.

Will the GNU-GCC compiler that's included with Code Blocks MingW recognize it?


I'm not sure what version is included. Try this code and see if it works:

c++:

#include <thread>
#include <iostream>

using namespace std;

void test()
{
    cout << "Thread 2" << endl;
}

int main()
{
    thread t(test);
    cout << "Thread 1" << endl;
    t.join();

    return 0;
}


Insectoid wrote:

It's a library. You install it and include it and it will work. On Windows. Some libraries only work on a specific operating system or architecture. conio.h is one of them.


Isn't thread platform-independent and installed by default, as part of the STL? Or were you talking specifically about conio.h?

Alright, I'll try it right after, but what does join for t.join do?
Edit: Nope, won't work.
code:
#error This file requires compiler and library support for the \
ISO C++ 2011 standard. This support is currently experimental, and must be \
enabled with the -std=c++11 or -std=gnu++11 compiler options.


You'll have to find the setting for compiler arguments in Code::Blocks and add -std=c++11.

t.join() waits for the thread to finish before continuing. If your main thread exits before a child thread, it will cause a mysterious error.
Cancer Sol




PostPosted: Wed Apr 03, 2013 7:31 pm   Post subject: Re: Start/Stop function using getch

nullptr @ 4/3/2013, 5:08 pm wrote:
Cancer Sol @ Wed Apr 03, 2013 1:06 pm wrote:
nullptr @ 4/2/2013, 9:50 pm wrote:
Cancer Sol @ Tue Apr 02, 2013 5:11 pm wrote:
nullptr @ 4/2/2013, 3:48 pm wrote:
Cancer Sol @ Tue Apr 02, 2013 10:55 am wrote:

. . .


As far as I know, the only way to get what you want is to use threads. See http://en.cppreference.com/w/cpp/thread/thread -- you'll need an up-to-date compiler like gcc 4.7. Otherwise, you'd need to wait for user input between each mouse click.

Will the GNU-GCC compiler that's included with Code Blocks MingW recognize it?


I'm not sure what version is included. Try this code and see if it works:

c++:

#include <thread>
#include <iostream>

using namespace std;

void test()
{
    cout << "Thread 2" << endl;
}

int main()
{
    thread t(test);
    cout << "Thread 1" << endl;
    t.join();

    return 0;
}


Insectoid wrote:

It's a library. You install it and include it and it will work. On Windows. Some libraries only work on a specific operating system or architecture. conio.h is one of them.


Isn't thread platform-independent and installed by default, as part of the STL? Or were you talking specifically about conio.h?

Alright, I'll try it right after, but what does join for t.join do?
Edit: Nope, won't work.
code:
#error This file requires compiler and library support for the \
ISO C++ 2011 standard. This support is currently experimental, and must be \
enabled with the -std=c++11 or -std=gnu++11 compiler options.


You'll have to find the setting for compiler arguments in Code::Blocks and add -std=c++11.

t.join() waits for the thread to finish before continuing. If your main thread exits before a child thread, it will cause a mysterious error.

Oh, I see. But why does this:
code:
thread t(test);

Has the function test in it anyways?
Is the output
code:

thread 2
thread 1
Sponsor
Sponsor
Sponsor
sponsor
nullptr




PostPosted: Wed Apr 03, 2013 8:32 pm   Post subject: Re: Start/Stop function using getch

Quote:

Oh, I see. But why does this:
code:
thread t(test);

Has the function test in it anyways?
Is the output
code:

thread 2
thread 1


When you construct a thread like that, you're telling the program to execute the function given, but in another thread (you can pass arguments to that function by giving them to the thread constructor). Both that thread and the current thread will execute at the same time -- so the output could be what you put, but it could also be
code:

Thread 1
Thread 2

and in fact, the output might vary as you run the program multiple times. Try this tutorial: http://www.codeproject.com/Articles/14746/Multithreading-Tutorial[/quote]
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: