-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKMRobot.cpp
255 lines (221 loc) · 4.75 KB
/
KMRobot.cpp
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
*
* KMRobot.cpp
* Build by Microsoft Visual Studio 2010
* ZhaoChunsheng 2016/08/26
* Needs Moxa PComm Lite - Serial Comm Development Tool
* http://www.moxa.com/product/download_pcommlite_info.htm
*
*/
#include "stdafx.h"
#include "KMRobot.h"
#include <Windows.h> // PComm.h needs this include file
#include "PCOMM\PComm.h" // Moxa PComm Lite
KMRobot::KMRobot(int port)
{
responseDelay=500;
this->port=port;
strcpy_s(str_mouseSpeed,"0.7645");
}
int KMRobot::Open()
{
int ret = sio_open (port);
if (ret == SIO_OK)
{
sio_ioctl (port, B9600, P_NONE | BIT_8 | STOP_1 );
return 1;
}
else
{
return 0;
}
}
void KMRobot::Close()
{
sio_close (port);
}
void KMRobot::KeyboardWrite(int key, int times)
{
char cmd[256]="Keyboard.write,";
char str_key[8]="";
char str_times[8]="";
_itoa_s(key,str_key,10);
_itoa_s(times,str_times,10);
strcat_s(cmd,str_key);
strcat_s(cmd,",");
strcat_s(cmd,str_times);
sio_write (port, cmd, strlen(cmd));
Sleep(responseDelay);
}
void KMRobot::KeyboardPrint(char str[512])
{
char cmd[512]="Keyboard.print,";
strcat_s(cmd,str);
sio_write (port, cmd, strlen(cmd));
Sleep(responseDelay);
}
void KMRobot::KeyboardPrintln(char str[512])
{
char cmd[512]="Keyboard.println,";
strcat_s(cmd,str);
sio_write (port, cmd, strlen(cmd));
Sleep(responseDelay);
}
void KMRobot::KeyboardPress(int key)
{
char cmd[256]="Keyboard.press,";
char str_key[8]="";
_itoa_s(key,str_key,10);
strcat_s(cmd,str_key);
sio_write (port, cmd, strlen(cmd));
Sleep(responseDelay);
}
void KMRobot::KeyboardRelease(int key)
{
char cmd[256]="Keyboard.release,";
char str_key[8]="";
_itoa_s(key,str_key,10);
strcat_s(cmd,str_key);
sio_write (port, cmd, strlen(cmd));
Sleep(responseDelay);
}
void KMRobot::KeyboardReleaseAll()
{
char cmd[256]="Keyboard.releaseAll";
sio_write (port, cmd, strlen(cmd));
Sleep(responseDelay);
}
void KMRobot::MousePress(char button[8])
{
char cmd[256]="Mouse.press,";
strcat_s(cmd,button);
sio_write (port, cmd, strlen(cmd));
Sleep(responseDelay);
}
void KMRobot::MouseRelease(char button[8])
{
char cmd[256]="Mouse.release,";
strcat_s(cmd,button);
sio_write (port, cmd, strlen(cmd));
Sleep(responseDelay);
}
void KMRobot::MouseClick(char button[8],int times)
{
char cmd[256]="Mouse.click,";
char str_times[8]="";
_itoa_s(times,str_times,10);
strcat_s(cmd,button);
strcat_s(cmd,",");
strcat_s(cmd,str_times);
sio_write (port, cmd, strlen(cmd));
Sleep(responseDelay);
}
void KMRobot::MouseMove(int x,int y, int wheel)
{
char cmd[256]="Mouse.move,";
char str_x[8]="";
char str_y[8]="";
char str_wheel[8]="";
_itoa_s(x,str_x,10);
_itoa_s(y,str_y,10);
_itoa_s(wheel,str_wheel,10);
strcat_s(cmd,str_x);
strcat_s(cmd,",");
strcat_s(cmd,str_y);
strcat_s(cmd,",");
strcat_s(cmd,str_wheel);
sio_write (port, cmd, strlen(cmd));
Sleep(responseDelay);
}
void KMRobot::MouseSetScreenPos(int x,int y)
{
char cmd[256]="Mouse.setPos,";
POINT pointScreen;
char str_x[8]="";
char str_y[8]="";
GetCursorPos(&pointScreen);
_itoa_s(pointScreen.x-x,str_x,10);
_itoa_s(pointScreen.y-y,str_y,10);
strcat_s(cmd,str_x);
strcat_s(cmd,",");
strcat_s(cmd,str_y);
strcat_s(cmd,",");
strcat_s(cmd,str_mouseSpeed);
sio_write (port, cmd, strlen(cmd));
Sleep(responseDelay + abs(pointScreen.x-x) + abs(pointScreen.y-y));
}
void KMRobot::MouseSetScreenPosEx(int x,int y)
{
POINT pointScreen;
MouseSetScreenPos(x,y);
do
{
GetCursorPos(&pointScreen);
if(pointScreen.x<x)
{
MouseMove(2,0,0);
}
else if(pointScreen.x>x)
{
MouseMove(-2,0,0);
}
if(pointScreen.y<y)
{
MouseMove(0,2,0);
}
else if(pointScreen.y>y)
{
MouseMove(0,-2,0);
}
}
while(pointScreen.x!=x || pointScreen.y!=y);
}
void KMRobot::MouseSetClientPos(int x,int y,int hwnd)
{
char cmd[256]="Mouse.setPos,";
POINT pointScreen,ptClient;
char str_x[8]="";
char str_y[8]="";
ptClient.x=x;
ptClient.y=y;
ClientToScreen(HWND(hwnd),&ptClient);
GetCursorPos(&pointScreen);
_itoa_s(pointScreen.x-ptClient.x,str_x,10);
_itoa_s(pointScreen.y-ptClient.y,str_y,10);
strcat_s(cmd,str_x);
strcat_s(cmd,",");
strcat_s(cmd,str_y);
strcat_s(cmd,",");
strcat_s(cmd,str_mouseSpeed);
sio_write (port, cmd, strlen(cmd));
Sleep(responseDelay + abs(pointScreen.x-ptClient.x) + abs(pointScreen.y-ptClient.y));
}
void KMRobot::MouseSetClientPosEx(int x,int y,int hwnd)
{
POINT pointScreen,ptClient;
MouseSetClientPos(x,y,hwnd);
ptClient.x=x;
ptClient.y=y;
ClientToScreen(HWND(hwnd),&ptClient);
do
{
GetCursorPos(&pointScreen);
if(pointScreen.x<ptClient.x)
{
MouseMove(2,0,0);
}
else if(pointScreen.x>ptClient.x)
{
MouseMove(-2,0,0);
}
if(pointScreen.y<ptClient.y)
{
MouseMove(0,2,0);
}
else if(pointScreen.y>ptClient.y)
{
MouseMove(0,-2,0);
}
}
while(pointScreen.x!=ptClient.x || pointScreen.y!=ptClient.y);
}