#include <windows.h> // WinAPI #include <iostream> // std::cout #include <unistd.h> // sleep(), usleep() #include <math.h>
// enum { menu=0, elm_1, points_cnt }; class MyBot { public: MyBot(); void run(); void move_to(int inx); void lclick_to(int inx); void rclick_to(int inx); void drag(int from_inx,int to_inx); POINT point[points_cnt]; };
MyBot::MyBot() : // ( ) point({ {100,100}, // 0 - menu {130,130}, // 1 - elm_1 }) { }
RegisterHotKey((HWND)Widget::winId(), 101, MOD_ALT, VK_F1); // RegisterHotKey((HWND)Widget::winId(), 102, MOD_ALT, VK_F2); // inx++ RegisterHotKey((HWND)Widget::winId(), 103, MOD_ALT, VK_F3); // inx RegisterHotKey((HWND)Widget::winId(), 104, MOD_ALT, VK_F4); // inx RegisterHotKey((HWND)Widget::winId(), 105, MOD_ALT, VK_F5); //
int inx=0; MyBot bot; bool Widget::nativeEvent(const QByteArray & eventType, void * message, long * result){ Q_UNUSED(result); Q_UNUSED(eventType); MSG* msg = reinterpret_cast<MSG*>(message); if(msg->message!=WM_HOTKEY)return false; switch(msg->wParam){ case 101: // Alt-F1 - bot.run(); return true; case 102: // Alt-F2 - inx++ if(inx<points_cnt-1)inx++; return true; case 103: // Alt-F3 - inx bot.move_to(inx); return true; case 104: // Alt-F4 - inx GetCursorPos(&bot.point[inx]); return true; case 105: // Alt-F5 - for(i=0;i<points_cnt;i++){ std::cout << "{" << bot.point[i].x << "," << bot.point[i].y << "}, //" << i << std::endl; } return true; } return false; }
#define width 1920 #define height 1080 void MyBot::move_to(int inx){ int x=point[id].x; int y=point[id].y; POINT pt; GetCursorPos(&pt); int from_x=pt.x; int from_y=pt.y; int to_x=x; int to_y=y; int dx=to_x-from_x; int dy=to_y-from_y; float fdx; float fdy; int loop_cnt; if(abs(dx)>abs(dy) && dx!=0){ fdx=dx<0? -1.0 :1.0; fdy=(float)dy/abs(dx); loop_cnt=abs(dx); } else if(dy!=0){ fdy=dy<0? -1.0 :1.0; fdx=(float)dx/abs(dy); loop_cnt=abs(dy); } else return; // 1 int time=1000000/loop_cnt; float fx=from_x; float fy=from_y; for(int i=0;i<loop_cnt;i++){ fy+=fdy; fx+=fdx; int nx=(fx)*(65536 / width); int ny=(fy)*(65536 / height); mouse_event(MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE,nx,ny,0,0); usleep(time); } usleep(50000); }
void MyBot::lclick_to(int inx){ move_to(inx); mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); usleep(50000); mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); usleep(100000); } void MyBot::rclick_to(int inx){ move_to(inx); mouse_event(MOUSEEVENTF_RIGHTDOWN,0,0,0,0); usleep(50000); mouse_event(MOUSEEVENTF_RIGHTUP,0,0,0,0); usleep(100000); }
void MyBot::drag(int from_inx, to_inx){ move_to(from_inx); usleep(50000); mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0); usleep(70000); move_to(to_inx); mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0); usleep(30000); }
void MyBot::run(){ rclick_to(menu); // lclick_to(elm_1); // }
Source: https://habr.com/ru/post/212779/
All Articles