Ir ao conteúdo
  • Cadastre-se

C++ Mouse Does Not Move Automatically When Pressing Left Button


isvkt
Ir à solução Resolvido por isvkt,

Posts recomendados

  • Solução

I'm coding a C++ program that makes a certain movement with the mouse by itself when I press the left mouse button, it has F1, F2, F3 keys and each key has a different movement, and then I press the left mouse button to execute that movement , but what is happening is that I have to hold down the F1 key and the left mouse button at the same time for it to work, I wanted to press the F1 key just once and then just hold down the left mouse button !

 

	while (true) {
		if (GetAsyncKeyState(VK_F1)) {
			ExecControl(*ak47);
		}
		if (GetAsyncKeyState(VK_F2)) {
			ExecControl(*m4a4);
		}
		if (GetAsyncKeyState(VK_F3)) {
			ExecControl(*m4a1s);
		}
		if (GetAsyncKeyState(VK_F4)) {
			ExecControl(*ump45);
		}
		if (GetAsyncKeyState(VK_F5)) {
			ExecControl(*famas);
		}
		Sleep(150);
	}

	return 0;
}

void ExecControl(weapon_t gun) {
	system("cls");

	cout <<"Weapon:\t" << gun.name <<"\nShots:\t" << gun.len << "\nVelocity:\t" << gun.rpm << "\n\n\n";

	DWORD delay = GetTime(gun.rpm);

	int index = 0;

		while (GetAsyncKeyState(VK_LBUTTON) && index != gun.len) {
			mouse_event(MOUSEEVENTF_MOVE, long(gun.pattner [index][0] * K), long(gun.pattner[index][1] * K), 0, 0);
			index++;
			Sleep(delay);
		}

		index = 0;
	}

 

Link para o comentário
Compartilhar em outros sites

@isvkt Whenever possible, post the complete and compilable code. Makes it easier for those trying to help you.

 

2 horas atrás, isvkt disse:

what is happening is that I have to hold down the F1 key and the left mouse button at the same time for it to work

Yes, you do.

But first of all, GetAsyncKeyState() returns 2 bits: the most significant indicates whether the key is pressed at the time of the call, and the least significant indicates whether the key has been pressed since the previous call to GetAsyncKeyState():

// Try clicking multiple times
Sleep(1000);
short x = GetAsyncKeyState(VK_LBUTTON);
printf("MSB: %d\nLSB: %d\nWas the key pressed at the time of the call? %s\n"
       "Has the key been pressed since the previous call? %s\n",
       ((x >> 15) & 1) ? 1 : 0, (x & 1) ? 1 : 0,
       ((x >> 15) & 1) ? "Yes" : "No", (x & 1) ? "Yes" : "No");

If you are lucky enough to be able to click at the time of the function call, the output will be:

image.png.91a4fc73d7942162a09f8cd012512770.png

Anyway, GetAsyncKeyState() doesn't always work, it can get "stuck" on modern computers if the user keeps pressing the key over and over repeatedly. The documentation also says:

Citação

Although the least significant bit of the return value indicates whether the key has been pressed since the last query, due to the preemptive multitasking nature of Windows, another application can call GetAsyncKeyState and receive the "recently pressed" bit instead of your application.

 

Now straight to the point: regarding the ExecControl() function, GetAsyncKeyState() does not wait for anything more than what it evaluates, and if none of the cases occurred to set any of the bits to 1, the result is 0.

2 horas atrás, isvkt disse:
while (GetAsyncKeyState(VK_LBUTTON) && index != gun.len)

Well, 0 && X = 0, the while condition becomes false and control is transferred out of the loop without further ado.

So yes, the function returns the control to where it was called, and if you want whatever is in the scope of the while of the ExecControl() function to be executed, you would have to find a way to press any of the two keys needed for it to work, or else fix the loop condition.

Knowing this, can you fix it? :)

  • Obrigado 1
Link para o comentário
Compartilhar em outros sites

22 minutos atrás, isvkt disse:

I did but it was not cool

Well, post the code you changed so we can see how it turned out.
If you don't want the while condition to be false if you don't press the left mouse button before or during the evaluation of the condition, you can use an if statement inside the while scope, just like you did in the function that calls ExecControl().

  • Curtir 1
Link para o comentário
Compartilhar em outros sites

Crie uma conta ou entre para comentar

Você precisa ser um usuário para fazer um comentário

Criar uma conta

Crie uma nova conta em nossa comunidade. É fácil!

Crie uma nova conta

Entrar

Já tem uma conta? Faça o login.

Entrar agora

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...