Sending a mouse input 마우스 입력 보내기
C, C++ 2019. 10. 28. 14:29 |반응형
The SendInput function inserts the events in the INPUT structures serially into the keyboard or mouse input stream.
마우스 입력을 보낸다.
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 | #include <Windows.h> #include <iostream> using namespace std; void PrintMenu() { cout << "--- Mouse sender ---" << endl << endl; cout << "0: End" << endl; cout << "1: Get cursor position" << endl; cout << "2: Send left button click" << endl; cout << "3: Send right button click" << endl; cout << "4: Send cursor move" << endl; cout << "Choose: "; } void GetCursorPosition() { Sleep(2000); // Wait for 2 seconds before get the cursor position. POINT cursorPos; GetCursorPos(&cursorPos); cout << "Current cursor position : (x: " << cursorPos.x << ", y: " << cursorPos.y << ")" << endl; } void SendLeftClick() { Sleep(5000); INPUT input; ZeroMemory(&input, sizeof(INPUT)); input.type = INPUT_MOUSE; input.mi.dwFlags = MOUSEEVENTF_LEFTDOWN; SendInput(1, &input, sizeof(INPUT)); input.mi.dwFlags = MOUSEEVENTF_LEFTUP; SendInput(1, &input, sizeof(INPUT)); } void SendRightClick() { Sleep(5000); INPUT input; ZeroMemory(&input, sizeof(INPUT)); input.type = INPUT_MOUSE; input.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN; SendInput(1, &input, sizeof(INPUT)); input.mi.dwFlags = MOUSEEVENTF_RIGHTUP; SendInput(1, &input, sizeof(INPUT)); } void SendMove() { int x, y; int screenWidth = GetSystemMetrics(SM_CXSCREEN); int screenHeight = GetSystemMetrics(SM_CYSCREEN); cout << "Screen size: (" << screenWidth << ", " << screenHeight << ")" << endl; cout << "[x: 0~" << screenWidth - 1 << ", y: 0~" << screenHeight - 1 << "]" << endl; cout << "x: "; cin >> x; cout << "y: "; cin >> y; INPUT input; ZeroMemory(&input, sizeof(INPUT)); input.type = INPUT_MOUSE; input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE; // 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; coordinate (65535,65535) // maps onto the lower-right corner. In a multimonitor system, the coordinates map to the primary monitor. // // If the MOUSEEVENTF_ABSOLUTE value is not specified, dxand dy specify movement relative to the previous // mouse event(the last reported position).Positive values mean the mouse moved right(or down); negative // values mean the mouse moved left(or up). float dx = x * (65535.0f / (screenWidth - 1)); float dy = y * (65535.0f / (screenHeight - 1)); input.mi.dx = LONG(dx); input.mi.dy = LONG(dy); SendInput(1, &input, sizeof(INPUT)); } int main() { int choice = 1; while (choice) { PrintMenu(); cin >> choice; switch (choice) { case 1: GetCursorPosition(); break; case 2: SendLeftClick(); break; case 3: SendRightClick(); break; case 4: SendMove(); break; default: return 0; } } return 0; } |
Run the program and choose the event you want to send.
반응형
'C, C++' 카테고리의 다른 글
Linux(Ubuntu) GTK+3(2) Image Display - 리눅스(우분투) GTK+3(2) 이미지 디스플레이 (0) | 2021.02.12 |
---|---|
Linux(Ubuntu) GTK+3(2) GUI Programming - 리눅스(우분투) GUI 프로그래밍 (0) | 2021.02.12 |
Sending a keyboard input 키 입력 보내기 (0) | 2019.10.27 |
How to set default arguments in a friend function - friend 함수 디폴트 매개 변수 설정 (0) | 2019.07.21 |
Integrated Color Picker for Visual Studio IDE and Editor (0) | 2019.07.05 |