使用SendInput发送Alt代码(Alt+小键盘1等)无输出问题求助
You’re spot on with your suspicion—the problem is definitely using the regular number key 1 instead of the numpad 1. Alt codes rely exclusively on the numpad digits; the top-row number keys won’t trigger this functionality, even with Num Lock enabled.
Here’s how to fix it properly:
Use the correct virtual key codes for numpad keys
The numpad 1 corresponds toVK_NUMPAD1(decimal value 97), whereas the regular 1 key isVK_1(49). Make sure you’re sending the numpad-specific virtual key when building your input sequence.Follow the strict Alt code timing sequence
Alt codes require precise key press/release order: press and hold Alt → press/release each numpad digit in sequence → release Alt. Proper delays between each action ensure the system recognizes the full sequence instead of individual keystrokes.
Working Code Example (C++)
#include <windows.h> void SendAltCode(int code) { // Press Alt key INPUT input = {0}; input.type = INPUT_KEYBOARD; input.ki.wVk = VK_MENU; // Virtual key code for Alt SendInput(1, &input, sizeof(INPUT)); Sleep(100); // Split the code into individual digits int digits[5] = {0}; int digitCount = 0; if (code == 0) { digits[digitCount++] = 0; } else { while (code > 0) { digits[digitCount++] = code % 10; code /= 10; } } // Send digits in reverse order (since we split from least significant digit) for (int i = digitCount - 1; i >= 0; --i) { // Press numpad digit input.ki.wVk = VK_NUMPAD0 + digits[i]; input.ki.dwFlags = 0; SendInput(1, &input, sizeof(INPUT)); Sleep(50); // Release numpad digit input.ki.dwFlags = KEYEVENTF_KEYUP; SendInput(1, &input, sizeof(INPUT)); Sleep(50); } // Release Alt key input.ki.wVk = VK_MENU; input.ki.dwFlags = KEYEVENTF_KEYUP; SendInput(1, &input, sizeof(INPUT)); Sleep(100); } int main() { Sleep(2000); // Give yourself time to switch to Notepad SendAltCode(1); // Send Alt+Numpad1 to input ☺ return 0; }
Key Notes:
- Keep Num Lock enabled: If Num Lock is off, numpad keys will act as arrow/navigation keys instead of digits, which breaks Alt code functionality.
- Adjust delays if needed: The
Sleep()calls give the system time to process each keystroke. Your existing 100ms delays are reasonable, but you can tweak them if you still experience issues (e.g., shorter delays might work on faster systems). - Don’t skip key releases: Every key press must have a corresponding release—failing to do so can leave keys "stuck" and cause unexpected behavior.
内容的提问来源于stack exchange,提问作者hurnhu

