Can you capture events like this when pressing a C++ key? [Codeblocks]

Asked

Viewed 663 times

1

Good morning, I am making an Autoclick bot for personal use and I would like to add the function of pausing clicks when pressing a specific key but I do not know if it is possible to do this in C++... well, if anyone can help me thank you. Obs Source Code below

#include <iostream>
#include <windows.h>

using namespace std;

int main(int argc, char * argv[]){

    //Declaracao das Variáveis a Serem Usadas <>
    int n = 1;
    int x;
    int y;
    int times;
    int done = 0;
    int sleepy;
    int botao;
    string choice;
    //Fim da Declaração de Variáveis </>

    //Declaração das funcoes a serem utilizadas <>
    void Clicar(int x, int y);
    void ClicarDireito(int x, int y);
    void ClicarMeio(int x, int y);
    void Digitar(char letra);
    //Fim da declaração de funções </>

    start:

//Inicio da coleta de dados <>
cout << "BOT_AutoClick Version 2.1" << endl;
cout << "   " << endl;

cout << "Quantas Vezes Deseja Clicar?" << endl;
cout << " : ";
cin >> times;

cout << "Quanto tempo deve ter entre os click? (Em Milisegundos)" << endl;
cout << "(MS): ";
cin >> sleepy;

cout << "Qual o botao a ser clicado : 1 - left / 2 - middle / 3 - right" <<     endl;
cout << " : ";
do{
cin >> botao;
}while( botao < 1 || botao > 3);

done = 0;
//Fim da Coleta de dados </>

//Contagem Regressiva para posicionar o mouse <>
cout << "starting in 5..." << endl;
Sleep(1000);
cout << "starting in 4..." << endl;
Sleep(1000);
cout << "starting in 3..." << endl;
Sleep(1000);
cout << "starting in 2..." << endl;
Sleep(1000);
cout << "starting in 1..." << endl;
Sleep(1000);
//Fim da Contagem Regressiva para posicionar o mouse </>

//Estrutura de Decisao para clicker C++ <>
if (botao == 1){

    while (done <= times)
    {
        Sleep(sleepy);
        Clicar(x, y); //Para Ver Função do click descer codigo
        done++;
    }
}

if (botao == 2){

        while (done <= times)
    {
        Sleep(sleepy);
        ClicarMeio(x, y); //Para Ver Função do click descer codigo
        done++;
    }
}

    if (botao == 3){

        while (done <= times){

        Sleep(sleepy);
        ClicarDireito(x, y); //Para Ver Função do click descer codigo
        done++;
        }
    }
    //Fim da Estrutura de Decisão do Clicker </>

    cout << "Processo Concluido com Sucesso!." << endl;
    cout << "BOT Autoclick - Criado por Mazurco066." << endl;
    cout << "    " << endl;
    Sleep(1000);
    cout << "Denovo?   sim(y) or nao(n)" << endl;
    cin >> choice;

    if (choice == "y")
    {

        system("cls");
        goto start;
    }

    cin.get();

}

void Clicar(int x, int y){

    mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}

void ClicarDireito(int x, int y){

    mouse_event(MOUSEEVENTF_RIGHTDOWN, x, y, 0, 0);
    mouse_event(MOUSEEVENTF_RIGHTUP, x, y, 0, 0);
}

void ClicarMeio(int x, int y){

    mouse_event(MOUSEEVENTF_MIDDLEDOWN, x, y, 0, 0);
    mouse_event(MOUSEEVENTF_MIDDLEUP, x, y, 0, 0);
}

void Digitar(char letra){

    keybd_event(VkKeyScan(letra),0x9e,0,0);
}

1 answer

2


Using only windows you can use the function Getasynckeystate(), that determina se uma tecla está apertada ou desapertada no momento em que a função é executada Implementing in your code would look like this:

#include <iostream>
#include <windows.h>

using namespace std;


//Declaração das funcoes a serem utilizadas <>
void Clicar(int x, int y);
void ClicarDireito(int x, int y);
void ClicarMeio(int x, int y);
void Digitar(char letra);
//Fim da declaração de funções </>

int main(int argc, char * argv[]){

//Declaracao das Variáveis a Serem Usadas <>
int n = 1;
int x;
int y;
int times;
int done = 0;
int sleepy;
int botao;
string choice;

unsigned long tempoEspera = 0;
unsigned long tempoEsperaClique = 0;
bool bPause = false;

//Fim da Declaração de Variáveis </>


start:

//Inicio da coleta de dados <>
cout << "BOT_AutoClick Version 2.1" << endl;
cout << "   " << endl;

cout << "Quantas Vezes Deseja Clicar?" << endl;
cout << " : ";
cin >> times;

cout << "Quanto tempo deve ter entre os click? (Em Milisegundos)" << endl;
cout << "(MS): ";
cin >> sleepy;

cout << "Qual o botao a ser clicado : 1 - left / 2 - middle / 3 - right" <<     endl;
cout << " : ";
do{
cin >> botao;
}while( botao < 1 || botao > 3);

done = 0;
//Fim da Coleta de dados </>

//Contagem Regressiva para posicionar o mouse <>
cout << "starting in 5..." << endl;
Sleep(1000);
cout << "starting in 4..." << endl;
Sleep(1000);
cout << "starting in 3..." << endl;
Sleep(1000);
cout << "starting in 2..." << endl;
Sleep(1000);
cout << "starting in 1..." << endl;
Sleep(1000);
//Fim da Contagem Regressiva para posicionar o mouse </>



//Estrutura de Decisao para clicker C++ <>
if (botao == 1){

    while (done <= times)
    {
        if(GetAsyncKeyState('v') & 0x8000)
        {
            if( tempoEspera < GetTickCount() )
            {
                tempoEspera = GetTickCount() + 350; //esperar 350 milisegundos
                bPause = !bPause; //troca de true pra false ou false pra true
            }
        }
        if(bPause == false && tempoEsperaClique < GetTickCount())
        {
            tempoEsperaClique = GetTickCount() + sleepy;
            Clicar(x, y); //Para Ver Função do click descer codigo
            done++;
        }
    }
}

if (botao == 2){

    while (done <= times)
    {
        if(GetAsyncKeyState('v') & 0x8000)
        {
            if( tempoEspera < GetTickCount() )
            {
                tempoEspera = GetTickCount() + 350; //esperar 350 milisegundos
                bPause = !bPause; //troca de true pra false ou false pra true
            }
        }
        if(bPause == false && tempoEsperaClique < GetTickCount())
        {
            tempoEsperaClique = GetTickCount() + sleepy;
            ClicarMeio(x, y); //Para Ver Função do click descer codigo
            done++;
        }
    }
}

    if (botao == 3){


        while (done <= times){

        if(GetAsyncKeyState('v') & 0x8000)
        {
            if( tempoEspera < GetTickCount() )
            {
                tempoEspera = GetTickCount() + 350; //esperar 350 milisegundos
                bPause = !bPause; //troca de true pra false ou false pra true
            }
        }
             if(bPause == false && tempoEsperaClique < GetTickCount())
            {
            tempoEsperaClique = GetTickCount() + sleepy;
            ClicarDireito(x, y); //Para Ver Função do click descer codigo
            done++;
            }
        }
    }
    //Fim da Estrutura de Decisão do Clicker </>

    cout << "Processo Concluido com Sucesso!." << endl;
    cout << "BOT Autoclick - Criado por Mazurco066." << endl;
    cout << "    " << endl;
    Sleep(1000);
    cout << "Denovo?   sim(y) or nao(n)" << endl;
    cin >> choice;

    if (choice == "y")
    {

        system("cls");
        goto start;
    }

    cin.get();

}

void Clicar(int x, int y){

    mouse_event(MOUSEEVENTF_LEFTDOWN, x, y, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, x, y, 0, 0);
}

void ClicarDireito(int x, int y){

    mouse_event(MOUSEEVENTF_RIGHTDOWN, x, y, 0, 0);
    mouse_event(MOUSEEVENTF_RIGHTUP, x, y, 0, 0);
}

void ClicarMeio(int x, int y){

    mouse_event(MOUSEEVENTF_MIDDLEDOWN, x, y, 0, 0);
    mouse_event(MOUSEEVENTF_MIDDLEUP, x, y, 0, 0);
}

void Digitar(char letra){

    keybd_event(VkKeyScan(letra),0x9e,0,0);
}

Notes:

Statement of duties to be made outside the scope main().

Sleep() it is highly not recommended because it 'hangs' or 'pauses' the entire execution of the program and it is not guaranteed to 'sleep' exactly the time spent. If you create a program with more threads it would pause the execution of the other threads as well.

Using GetTickCount() you can give intervals in milliseconds without pausing the execution of the program.

It is also necessary to use GetTickCount() by pressing a key to pause the clicks or not because if a delay is not used between pressing the keys, the execution speed of the program bPause = !bPause would run several times at a quick keystroke.

  • vlw guy solved it. I was using another key capture method while waiting but was bad the pause method. thanks msm

Browser other questions tagged

You are not signed in. Login or sign up in order to post.