Sending a keyboard input 키 입력 보내기
C, C++ 2019. 10. 27. 21:41 |반응형
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 | #include <Windows.h> #include <iostream> using namespace std; void PrintMenu() { cout << "--- Key sender ---" << endl << endl; cout << "0: End" << endl; cout << "1: Send left key" << endl; cout << "2: Send right key" << endl; cout << "3: Send up key" << endl; cout << "4: Send down key" << endl; cout << "5: Send enter key" << endl; cout << "6: Send 'aA'" << endl; cout << "Choose: "; } // Virtual-Key Codes: https://docs.microsoft.com/ko-kr/windows/win32/inputdev/virtual-key-codes void SendVKcodes(BYTE vk) { Sleep(5000); // Wait for 5 seconds before sending a keycode. INPUT input; ZeroMemory(&input, sizeof(INPUT)); input.type = INPUT_KEYBOARD; input.ki.wVk = vk; // A virtual-key code. The code must be a value in the range 1 to 254. If the dwFlags member // specifies KEYEVENTF_UNICODE, wVk must be 0. // input.ki.wScan = vk; // A hardware scan code for the key. If dwFlags specifies KEYEVENTF_UNICODE, wScan specifies // a Unicode character which is to be sent to the foreground application. SendInput(1, &input, sizeof(INPUT)); input.ki.dwFlags = KEYEVENTF_KEYUP; // If specified, the key is being released. If not specified, the key is being pressed. SendInput(1, &input, sizeof(INPUT)); } void SendaA() { Sleep(5000); // Send 'a' INPUT input; ZeroMemory(&input, sizeof(INPUT)); input.type = INPUT_KEYBOARD; input.ki.wVk = 0x41; // a key SendInput(1, &input, sizeof(INPUT)); input.ki.dwFlags = KEYEVENTF_KEYUP; SendInput(1, &input, sizeof(INPUT)); // Sned 'A' input.ki.dwFlags = 0; input.ki.wVk = VK_SHIFT; SendInput(1, &input, sizeof(INPUT)); input.ki.wVk = 0x41; // a key SendInput(1, &input, sizeof(INPUT)); input.ki.dwFlags = KEYEVENTF_KEYUP; SendInput(1, &input, sizeof(INPUT)); input.ki.wVk = VK_SHIFT; SendInput(1, &input, sizeof(INPUT)); } int main() { int choice = 1; while (choice) { PrintMenu(); cin >> choice; switch (choice) { case 1: SendVKcodes(VK_LEFT); break; case 2: SendVKcodes(VK_RIGHT); break; case 3: SendVKcodes(VK_UP); break; case 4: SendVKcodes(VK_DOWN); break; case 5: SendVKcodes(VK_RETURN); break; case 6: SendaA(); break; default: return 0; } } return 0; } |
Run the program and choose the key you want to send.
※ Adjust key down duration by adding Sleep() between KEYEVENTF_KEYDOWN and KEYEVENTF_KEYUP.
For short key down duration, give 60~100 to Sleep() for the better result although some keys don't need Sleep() at all for very short key down duration.
반응형
'C, C++' 카테고리의 다른 글
Linux(Ubuntu) GTK+3(2) GUI Programming - 리눅스(우분투) GUI 프로그래밍 (0) | 2021.02.12 |
---|---|
Sending a mouse input 마우스 입력 보내기 (0) | 2019.10.28 |
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 |
How to open a command prompt on the project's path in Visual Studio (2) | 2019.07.03 |