如何在C++(Windows平台)中打开外部exe并模拟按键输入?
Hey there! I totally get where you're coming from—helping a family member simplify repetitive tasks is such a nice gesture, and it's totally doable with the Windows API even if you're new to C++. Let's walk through this step by step.
Core Approach: Use SendInput from windows.h
You were on the right track with windows.h! The key API you need is SendInput—it's the modern, reliable way to simulate keyboard input on Windows (way better than the older keybd_event which is deprecated). Here's how to put it together:
Step 1: Basic Setup & Helper Functions
First, we'll create simple helper functions to handle single key presses and typing full strings. These will make your main code clean and easy to follow.
#include <windows.h> #include <string> // Helper to press and release a single key void PressKey(WORD keyCode) { INPUT input = {0}; input.type = INPUT_KEYBOARD; input.ki.wVk = keyCode; // Press the key SendInput(1, &input, sizeof(INPUT)); // Release the key input.ki.dwFlags = KEYEVENTF_KEYUP; SendInput(1, &input, sizeof(INPUT)); } // Helper to type a full string (handles uppercase, symbols too!) void TypeString(const std::string& text) { for (char c : text) { // Convert character to virtual key code + shift state SHORT keyInfo = VkKeyScanA(c); WORD keyCode = LOBYTE(keyInfo); bool needsShift = (HIBYTE(keyInfo) & 1) != 0; // Press Shift if needed (for uppercase/symbols like !@#) if (needsShift) { INPUT shiftInput = {0}; shiftInput.type = INPUT_KEYBOARD; shiftInput.ki.wVk = VK_SHIFT; SendInput(1, &shiftInput, sizeof(INPUT)); } // Press and release the character key PressKey(keyCode); // Release Shift if we pressed it if (needsShift) { INPUT shiftInput = {0}; shiftInput.type = INPUT_KEYBOARD; shiftInput.ki.wVk = VK_SHIFT; shiftInput.ki.dwFlags = KEYEVENTF_KEYUP; SendInput(1, &shiftInput, sizeof(INPUT)); } } }
Step 2: Main Logic for Your Automation
Now, the main function will handle the sequence you want: username → TAB → password → ENTER. We'll add a short delay to let your family member switch to the target program first.
int main() { // Wait 3 seconds so the user can switch to the program they want to input into Sleep(3000); // Type the username TypeString("username"); // Press Tab to move to password field PressKey(VK_TAB); // Type the password TypeString("password"); // Press Enter to submit PressKey(VK_RETURN); return 0; }
Important Notes for Compilation & Usage
- Link the required library: When compiling with MSVC, you need to link
user32.lib(whereSendInputlives). You can either add#pragma comment(lib, "user32.lib")at the top of your code, or add/link user32.libto your compile command. - Run with correct permissions: If the target program is running as administrator, your automation program also needs to run as admin—otherwise it might not be able to send input to the window.
- Testing tip: Start small! Test just typing a single character first, then add the Tab and Enter steps once that works. This helps you debug if something goes wrong.
- Alternative for stubborn programs: Some apps (like security tools or games) block
SendInput. If that happens, you can try sendingWM_CHARmessages directly to the input field's window handle, but that requires finding the specific input box (usingFindWindowEx), which is a bit more advanced. Let me know if you need help with that!
Hope this gets you up and running smoothly. Take it slow, and don't hesitate to tweak the delay or adjust the functions to fit your family member's exact needs.
内容的提问来源于stack exchange,提问作者gabelepoudre




