#include <time.h>
#include <stdio.h>
#include <Windows.h>
HANDLE ghMutex;
///////////////////////////////////////////////////////////////////////////////////
/* 移动鼠标的位置(相对)
* the amount of motion since the last mouse event was generated
* @dx : relative x coordinate is specified as the number of pixels moved
* @dy : relative y coordinate is specified as the number of pixels moved
*/
int MoveMouse(LONG dx, LONG dy)
{
UINT ret;
INPUT input = { 0 };
input.type = INPUT_MOUSE;
input.mi.time = 0;//the system will provide time stamp
input.mi.dwFlags = MOUSEEVENTF_MOVE; //Movement occurred.
input.mi.dx = dx;
input.mi.dy = dy;
//send Mouse Event
ret = SendInput(1, &input, sizeof(INPUT));
if (ret != 1) {
printf("SendInput failed, %ld\n", GetLastError());
return -1;
}
//Assumes the mouse sampling rate is 8ms
Sleep(8);
return 0;
}
//////////////////////////////////////////////////////////////////////////////////////
enum MouseButtonType {
BUTTON_LEFT, //鼠标左键
BUTTON_RIGHT, //鼠标右键
BUTTON_MID //鼠标中键(滚轮)
};
enum MouseClickType {
BUTTON_PRESS, //按键压下
BUTTON_RELEASE, //按键抬起
BUTTON_CLICK //按键点击(压下后抬起)
};
int ClickMouse(MouseButtonType ButtonType, MouseClickType ClickType)
{
UINT ret;
UINT count = 2;
INPUT input[2] = { 0 };
struct MouseOperArr {
MouseButtonType button;
struct MouseEvtFlg {
UINT flgCnt;
DWORD dwFlags[2];
}flags[3];
} MouseEvtArr[3] = {
{ BUTTON_LEFT, { { 1, { MOUSEEVENTF_LEFTDOWN, 0 } }, \
{1, { MOUSEEVENTF_LEFTUP, 0 }}, {2, { MOUSEEVENTF_LEFTDOWN, MOUSEEVENTF_LEFTUP } } } },
{ BUTTON_RIGHT, { { 1, { MOUSEEVENTF_RIGHTDOWN, 0 } }, \
{1, { MOUSEEVENTF_RIGHTUP, 0 }}, { 2, { MOUSEEVENTF_RIGHTDOWN, MOUSEEVENTF_RIGHTUP } } } },
{ BUTTON_MID, { { 1, { MOUSEEVENTF_MIDDLEDOWN, 0 } }, \
{1, { MOUSEEVENTF_MIDDLEUP, 0 }}, { 2,{ MOUSEEVENTF_MIDDLEDOWN, MOUSEEVENTF_MIDDLEUP } } } }
};
//common data
input[0].type = INPUT_MOUSE;
input[0].mi.time = 0;//the system will provide time stamp
input[1].type = INPUT_MOUSE;
input[1].mi.time = 0;//the system will provide time stamp
count = MouseEvtArr[ButtonType].flags[ClickType].flgCnt;
input[0].mi.dwFlags = MouseEvtArr[ButtonType].flags[ClickType].dwFlags[0];
input[1].mi.dwFlags = MouseEvtArr[ButtonType].flags[ClickType].dwFlags[1];
//send Mouse Event
ret = SendInput(count, input, sizeof(INPUT));
if (ret != count) {
printf("SendInput failed, %ld\n", GetLastError());
return -1;
}
//Assumes the mouse sampling rate is 8ms
Sleep(8);
return 0;
}
#define HK_ALT_F1 (1001)
#define HK_ALT_F2 (1002)
#define HK_ALT_F3 (1003)
DWORD WINAPI ThreadEntry(LPVOID lpThreadParameter)
{
srand(time(NULL));
while (1) {
//wait for message to startup
WaitForSingleObject(ghMutex, INFINITE);
//压下左键不松手
// ClickMouse(BUTTON_LEFT, BUTTON_PRESS);
int number = rand() % 10;
int dir = (number > 5 ? 1 : -1);
printf("Move\n");
MoveMouse(dir * number, dir * number);
//release mutex for new message
ReleaseMutex(ghMutex);
Sleep(2000);
}
return 0;
}
int main(void)
{
//注册热键 Alt + F1,用于启动程序
RegisterHotKey(NULL, HK_ALT_F1, MOD_ALT | MOD_NOREPEAT, VK_F1);
//注册热键 Alt + F2,用于暂停程序
RegisterHotKey(NULL, HK_ALT_F2, MOD_ALT | MOD_NOREPEAT, VK_F2);
//注册热键 Alt + F2,用于退出程序
RegisterHotKey(NULL, HK_ALT_F3, MOD_ALT | MOD_NOREPEAT, VK_F3);
ghMutex = CreateMutex(NULL, FALSE, NULL);
WaitForSingleObject(ghMutex, INFINITE);
//创建工作线程
HANDLE handle = CreateThread(NULL, 0, ThreadEntry, NULL, 0, NULL);
//主线程 用于接收热键消息
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0)) {
switch (msg.message) {
case WM_HOTKEY:
switch (msg.wParam) {
case HK_ALT_F1://接收到ALT + F1
ReleaseMutex(ghMutex);
break;
case HK_ALT_F2://接收到ALT + F2
WaitForSingleObject(ghMutex, INFINITE);
break;
case HK_ALT_F3:
goto FINISH;
break;
default:
break;
}
break;
case WM_QUIT:
case WM_CLOSE:
goto FINISH;
break;
default:
break;
}
}
FINISH:
CloseHandle(ghMutex);
UnregisterHotKey(NULL, HK_ALT_F1);
UnregisterHotKey(NULL, HK_ALT_F2);
UnregisterHotKey(NULL, HK_ALT_F3);
//close worker thread
TerminateThread(handle, 0);
WaitForSingleObject(handle, INFINITE);
CloseHandle(handle);
return 0;
}