Rapid Fire

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <Windows.h>

using namespace std;
int main()
{
	while(true)
	{
		if(GetAsyncKeyState(VK_LBUTTON)&0x8000)
		{
			mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 10, 0, 0);
			Sleep(60);
			mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
			Sleep(20);
			
		}
	}
return 0;
}


Is there any way I can get a separate left click function only affected by the mouse, because it seems these two cancel each other out
 
mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);

and
 
if(GetAsyncKeyState(VK_LBUTTON)&0x8000);
Is there a way to explain what the actual goal here is in another way? What are you trying to do overall?
And do you want your solution to work just on a console or do you want a window?

PS: I have not used mouse_event before, but from a google search it sounds like it's a very outdated function.
I want to do "Minecraft Clicker" in QT, I know sth about gui, but this problem is a pause for my project.

Left + R = Bool true,
Left + True Bool = Clicking,
Left released = Bool false.
Last edited on
Still not exactly sure what what that means. Do you just want a console window that waits for input and then when it detects particular input combinations, it then simulates a click in another application?

And you want a Left mouse button to cause a click event to happen (but only after previously pressing Left mouse + R?)

To start, if you want to simulate a left click (left mouse down and up events), this will automatically do a double-click on global coordinate (20, 20) of your screen. (So on my Windows machine, it double-clicks and opens the Recycle bin).
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <windows.h>

// Partially copied from http://www.cplusplus.com/forum/lounge/17053/

//
// Desc    : Clicks the left mouse button down and releases it.
// Returns : Nothing.
//
void LeftClick()
{  
	INPUT    Input={0};								// Create our input.

	Input.type        = INPUT_MOUSE;				// Let input know we are using the mouse.
	Input.mi.dwFlags  = MOUSEEVENTF_LEFTDOWN;		// We are setting left mouse button down.
	SendInput( 1, &Input, sizeof(INPUT) );			// Send the input.

	ZeroMemory(&Input,sizeof(INPUT));				// Fills a block of memory with zeros.
	Input.type        = INPUT_MOUSE;				// Let input know we are using the mouse.
	Input.mi.dwFlags  = MOUSEEVENTF_LEFTUP;			// We are setting left mouse button up.
	SendInput( 1, &Input, sizeof(INPUT) );			// Send the input.
}

//
// Desc    : Gets the cursors current position on the screen.
// Returns : The mouses current on screen position.
// Info    : Used a static POINT, as sometimes it would return trash values
//
POINT GetMousePosition()
{
	static POINT m;
	POINT mouse;
	GetCursorPos(&mouse);
	m.x = mouse.x;
	m.y = mouse.y;
	return m;
}

//
// Desc    : Sets the cursors position to the point you enter (POINT& mp).
// Returns : Nothing.
//
void SetMousePosition(POINT& mp)
{
	long fScreenWidth	    = GetSystemMetrics( SM_CXSCREEN ) - 1; 
	long fScreenHeight	    = GetSystemMetrics( SM_CYSCREEN ) - 1; 

	// http://msdn.microsoft.com/en-us/library/ms646260(VS.85).aspx
	// If MOUSEEVENTF_ABSOLUTE value is specified, dx and dy contain normalized absolute coordinates between 0 and 65,535.
	// The event procedure maps these coordinates onto the display surface.
	// Coordinate (0,0) maps onto the upper-left corner of the display surface, (65535,65535) maps onto the lower-right corner.
	float fx		        = mp.x * ( 65535.0f / fScreenWidth  );
	float fy		        = mp.y * ( 65535.0f / fScreenHeight );		  
				
	INPUT Input             = { 0 };			
	Input.type		        = INPUT_MOUSE;

	Input.mi.dwFlags	    = MOUSEEVENTF_MOVE|MOUSEEVENTF_ABSOLUTE;
				
	Input.mi.dx		        = (long)fx;
	Input.mi.dy		        = (long)fy;

	SendInput(1, &Input,sizeof(INPUT));
}

int main()
{

    POINT point;
    point.x = 20;
    point.y = 20;
    SetMousePosition(point);
    
    // double click
    LeftClick();
    LeftClick();

}


For your purposes, the above code probably has to be combined with some button-handling logic from your post.
Last edited on
I want to do "Minecraft Clicker" in QT, I know sth about gui, but this problem is a pause for my project.



Just wondering why you have posted Windows code, if you want to do Qt ?
It's easiest example of my problem..

I want an app what works in background, and when I toggling bool (press LMB and R) that will spam LMB (bool active - holding only LMB, released R), while LMB released (bool disabled) will not clicking.

It does not matter if it will click in the Minecraft window or globally.

Here is my function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void click::loop()
    {
        static long xPos = 0, yPos= 0;

        if(GetAsyncKeyState(0x52) && GetAsyncKeyState(0x01))
        {
            rLMB = true;
        }
        if(GetAsyncKeyState(0x01) == 0)
        {
            rLMB = false;
            qDebug() << delay;
        }
        if(rLMB && GetAsyncKeyState(0x01))
        {
            qDebug() << "Left";
            mouse_event(MOUSEEVENTF_LEFTDOWN, xPos, yPos, 0, 0);
        }
    }


Yes, I know, It should HOLD the button (In minecraft it's clicking normally) but globally, its holding.
Last edited on
It's easiest example of my problem..


But you will just create confusion if you continue to write windows code.

As far as I understand it, one writes everything with Qt classes and functions, no need to write any windows code at all. And Qt does have functions and classes for everything, even to the point of using QString rather than std::string.

http://doc.qt.io/qt-5/classes.html

This helps with making Qt code portable: if there is windows code, one couldn't port the code to Linux or Mac say.

Also you shouldn't have to write your own loop, Qt has internal state machines and events.
Ok, thanks for advice, but firstly, I want to create this project for windows (later maybe portable).
(later maybe portable).


As long as you understand that those two things are completely different. And it will be probably be easier in Qt, in the same way that .NET is easier than Win32.
Clicked only one time.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    void click::loop()
    {

        //static long xPos = 0, yPos= 0;

        if(GetAsyncKeyState(0x52) && GetAsyncKeyState(0x01))
        {
            rLMB = true;
        }
        if(GetAsyncKeyState(0x01) == 0)
        {
            rLMB = false;
            qDebug() << delay;
        }
        if(rLMB && GetAsyncKeyState(0x01))
        {
            qDebug() << "Left";
            LeftClick();
        }
    }


PostMessage work's well.
Last edited on
Topic archived. No new replies allowed.