Simulation of mouse click does not function correctly

I am currently trying to develop an auto-clicker. However, it does not seem to work. I am quite new to C++, so there may be some obvious solutions I have not tried. The following code is all in a while(1) loop:

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
 if (GetAsyncKeyState('R')) {
				click = true;
			}
			if (GetAsyncKeyState('F')) {
				click = false;
			}
			if (GetAsyncKeyState(VK_DELETE)) {
				abort();
			}

			

			
				if ((GetAsyncKeyState(VK_LBUTTON) & 0x80) != 0) {
					mwdown = true;
				}
				else { mwdown = false; }
			
			if (click == true) {
				if (mwdown == true) {
				mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
				mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
				Sleep((rand() % 50) + 30);
				if (GetAsyncKeyState('F')) {
					click = false;

					if (GetAsyncKeyState(VK_DELETE)) {
						abort();
					}
					if (GetAsyncKeyState(VK_DELETE)) {
						abort();
					}
					}
				} if (GetAsyncKeyState('P')) {
					if (hidden == false) {
						HideConsole();
						hidden = true;
						Sleep(1000);
					}
					else {
						ShowConsole();
						hidden = false;
						Sleep(1000);
					}
				}
			}


I was expecting to click R and hold down the left mouse button as my mouse would have clicked. However, no clicks happen. I believe this happens as holding down the left mouse button interferes with the click.

If anyone could tell me how to make it work, I would be very grateful. Also, if there is a way to make this both activated and deactivated by pressing R, I would appreciate somebody telling me how to do it. Thanks.
Last edited on
Actually take a look here:

http://www.cplusplus.com/forum/beginner/246871/#msg1093182

The trick is that you need to use message handler instead of [while] loops
Tried that, doesn't seem to work. Maybe I am doing something incorrectly? If possible, could you show me how to do it with my code?
bump
I think that this is console? Then a while loop is supposed to be ok.

So, how do you determine that there are clicks?

Notice that the clicks come very rapidly. Max 80 milliseconds.

Furthermore, you may add debug output to see whether your logic does what you intend.
I have tried a debug output. When I hold down the button, it only prints "de". However, as soon as I hit 'F' and make 'click = false', it finishes printing "debug". This only happens once.
First thing is that things happen only as long as 'R' is pressed. When 'R' is released nothing will happen anymore. Is this your intention?

When I hold down the button, it only prints "de"
Since the loop is very fast, even pressing the key shortly means some thousands of cycles. This may be too much für the console output.

However, as soon as I hit 'F'
Only while 'R' is hold down 'F' is checked. So you need to hold down both keys to have an effect.
Topic archived. No new replies allowed.