Simple Mouse Program

Nov 13, 2009 at 12:35am
Ok, so my code is horrible I know. I am inexperienced and this was the best I could do with my knowledge. Basically the function of the program is to record the mouse's x and y coordinates and whether the left mouse button or the right mouse button has been pressed, then play it back. Right now it works, but it is horribly inefficient and I could really use some of your expert advise on how to best change it.There are a lot of issues and I know it, but here it is:

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//this is a mouse macro program
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <iostream>

using std::cout;


POINT p; //pointer for position (global)



void show() {
HWND hwnd = GetConsoleWindow();
SetWindowPos(
    hwnd,
    HWND_TOPMOST,
    25,
    25,
    110,
    200,
    0
);
}

void checkpos() {
GetCursorPos(&p); //get current mouse position
}


void LclickD() {
mouse_event(MOUSEEVENTF_LEFTDOWN , 0, 0, 0, 0); // Left click Down
}
void LclickU() {
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);// Left click up
}
void RclickD() {
mouse_event(MOUSEEVENTF_RIGHTDOWN , 0, 0, 0, 0); // Right click down
}
void RclickU() {
mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0); // Right click up
}


int main() {//

const int con = 1000; // the array sizes

int mclick[con]; //changing left or righ click into a number
int xpos[con]; //click x position array
int ypos[con]; //click y position array
int clicknum = 0;
int tvarx = 0;
int tvary = 0;
int crnt = 0;
int tvar = 0;
int cur = 0; //current number in array
int tx = 0;
int ty = 0;
int x = 0;

show();
// I didn't have to do all of these newlines but I decided to make the window
// pretty small, and I was thinking I may just hide the window while it is 
// recording- but there are bigger issues

cout <<"this is a\nmouse \nrecording\nprogram \nhold CTRL \nto Record \nthen hit \nescape to \nplay back";

while (1) {
cur = 0;
clicknum = 0;


    while (cur < con) {


if (GetAsyncKeyState(VK_ESCAPE) != 0) {//
                                 break;
                                 }//if

    if (GetAsyncKeyState(VK_CONTROL) != 0) {


if((GetAsyncKeyState(VK_LBUTTON) != 0) && (GetAsyncKeyState(VK_RBUTTON) != 0)){
                                 mclick[cur] = 3;
                                 }
else if((GetAsyncKeyState(VK_LBUTTON) != 0)){
                                 mclick[cur] = 1;
                                 }
else if((GetAsyncKeyState(VK_RBUTTON) != 0)){
                                 mclick[cur] = 2;
                                 }
else {
                                 mclick[cur] = 0;
                                 }
checkpos();

cout << "\n" << p.x << " " << p.y << " " << mclick[cur] << "\n"; //output x and y coordinates
cout << cur << "/" << con << "\n"; //output the current array number/total number


xpos[cur] = p.x; // input current mouse position int array
ypos[cur] = p.y; //                ''

cur++; // increment the array number


}
Beep(10,10); // this was for debugging early on
Sleep (100);
}



clicknum = cur;
cur = 0;
for (int z = 0; z < clicknum; z++){//



tvar = 0;
SetCursorPos(xpos[cur],ypos[cur]); // set the mouse to current array state


cout << xpos[cur] << "  " << ypos[cur] << "  " << mclick[cur] << "\n"; // output mouse state

//This is one of the bigger issues with the program, but I'm not sure how to proceed without
//using the x variable which I would like to demolish

 if (x == 1 && mclick[cur] != 1){  
       if ((mclick[cur] != 1) && (mclick[cur] != 3)) 
       {
                        LclickU();

                        x = 0;
                        }
                        }
 else if (x == 2 && mclick[cur] != 1){
       if ((mclick[cur] != 2) || (mclick[cur] != 3))
       {

                        RclickU();
                        x = 0;
                        }
                        }
 else if (x == 3 && mclick[cur] != 3){
       if ((mclick[cur] != 1 || mclick[cur] != 2) || (mclick[cur] != 3))
       {


                        LclickU();
                        RclickU();
                        x = 0;
                        }
                        }

     if (mclick[cur] == 1){
     LclickD();

     x = 1;
     }
     else if (mclick[cur] == 2){
          RclickD();

          x = 2;
          }
     else if (mclick[cur] == 3){
          LclickD();
          RclickD();


          x = 3;
          }


         cur++;
      Sleep(50);
      }


}
// We should never reach this
system("pause");

return 0;

}


That's that, Thank you for getting this far, I hope you can help!
Nov 13, 2009 at 1:14am
Jesus, what's wrong with your indentation?
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//this is a mouse macro program
#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <iostream>

using std::cout;


POINT p; //pointer for position (global)
void show()
{
    HWND hwnd = GetConsoleWindow();

    SetWindowPos(
        hwnd,
        HWND_TOPMOST,
        25,
        25,
        110,
        200,
        0
        );
}

void checkpos()
{
    GetCursorPos(&p); //get current mouse position
}


void LclickD()
{
    mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); // Left click Down
}
void LclickU()
{
    mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); // Left click up
}
void RclickD()
{
    mouse_event(MOUSEEVENTF_RIGHTDOWN, 0, 0, 0, 0); // Right click down
}
void RclickU()
{
    mouse_event(MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0); // Right click up
}


int main(){
    const int con = 1000; // the array sizes

    int mclick[con];//changing left or righ click into a number
    int xpos[con];//click x position array
    int ypos[con];//click y position array
    int clicknum = 0;
    int tvarx = 0;
    int tvary = 0;
    int crnt = 0;
    int tvar = 0;
    int cur = 0; //current number in array
    int tx = 0;
    int ty = 0;
    int x = 0;

    show();
    // I didn't have to do all of these newlines but I decided to make the window
    // pretty small, and I was thinking I may just hide the window while it is
    // recording- but there are bigger issues

    cout << "this is a\nmouse \nrecording\nprogram \nhold CTRL \nto Record \nthen hit \nescape to \nplay back";

    while (1) {
        cur = 0;
        clicknum = 0;


        while (cur < con) {
            if (GetAsyncKeyState(VK_ESCAPE) != 0) //
                break;
                    //if

            if (GetAsyncKeyState(VK_CONTROL) != 0) {
                if ((GetAsyncKeyState(VK_LBUTTON) != 0) && (GetAsyncKeyState(VK_RBUTTON) != 0))
                    mclick[cur] = 3;
                else if ((GetAsyncKeyState(VK_LBUTTON) != 0))
                    mclick[cur] = 1;
                else if ((GetAsyncKeyState(VK_RBUTTON) != 0))
                    mclick[cur] = 2;
                else
                    mclick[cur] = 0;
                checkpos();

                cout << "\n" << p.x << " " << p.y << " " << mclick[cur] << "\n";//output x and y coordinates
                cout << cur << "/" << con << "\n";//output the current array number/total number


                xpos[cur] = p.x;// input current mouse position int array
                ypos[cur] = p.y;//''

                cur++;// increment the array number
            }
            Beep(10, 10); // this was for debugging early on
            Sleep(100);
        }



        clicknum = cur;
        cur = 0;
        for (int z = 0; z < clicknum; z++) { //
            tvar = 0;
            SetCursorPos(xpos[cur], ypos[cur]); // set the mouse to current array state


            cout << xpos[cur] << "" << ypos[cur] << "" << mclick[cur] << "\n"; // output mouse state

    //This is one of the bigger issues with the program, but I'm not sure how to proceed without
    //using the x variable which I would like to demolish

            if (x == 1 && mclick[cur] != 1) {
                if ((mclick[cur] != 1) && (mclick[cur] != 3)) {
                    LclickU();

                    x = 0;
                }
            } else if (x == 2 && mclick[cur] != 1) {
                if ((mclick[cur] != 2) || (mclick[cur] != 3)) {
                    RclickU();
                    x = 0;
                }
            } else if (x == 3 && mclick[cur] != 3) {
                if ((mclick[cur] != 1 || mclick[cur] != 2) || (mclick[cur] != 3)) {
                    LclickU();
                    RclickU();
                    x = 0;
                }
            }

            if (mclick[cur] == 1) {
                LclickD();

                x = 1;
            } else if (mclick[cur] == 2) {
                RclickD();

                x = 2;
            } else if (mclick[cur] == 3) {
                LclickD();
                RclickD();


                x = 3;
            }


            cur++;
            Sleep(50);
        }
    }
    // We should never reach this
    system("pause");

    return 0;
}
Last edited on Nov 13, 2009 at 1:15am
Nov 13, 2009 at 1:22am
As I said I am really not experienced, sorry! Pointers? (no pun intended)
Nov 28, 2009 at 8:28am
It was the GetAsyncKeyState command that was slowing everything down, I believe it has a queue between key checks. So perhaps a change from checking the keystate every loop, instead do it once every five or so loops.
Topic archived. No new replies allowed.