代码之家  ›  专栏  ›  技术社区  ›  BullyWiiPlaza

在Windows上与SendInput函数一起使用字符串

  •  0
  • BullyWiiPlaza  · 技术社区  · 6 年前

    std::string 对于 SendInput()

    经过研究,我发现 VkKeyScanEx 班次 按键,发送小写键输入,然后它会显示为大写。不过,也有一些“特殊”的人物喜欢 !"§$%&/()=? 班次 关键是要正确出来。此外,还有类似的元音 äüö . 我下面的代码可以工作,但元音也是大写的,这是错误的,例如(因为 isalnum()

    const auto key_board_layout = GetKeyboardLayout(0);
    
    void configure_input(INPUT& input)
    {
        input.type = INPUT_KEYBOARD;
        input.ki.wScan = 0;
        input.ki.time = 0;
        input.ki.dwExtraInfo = 0;
        input.ki.dwFlags = 0;
    }
    
    void send_input(const HWND window_handle, const std::string& message)
    {
        SetForegroundWindow(window_handle);
        std::locale::global(std::locale("")); // Needed to not crash on certain special characters      
    
        for (auto character : message)
        {
            INPUT key_input;
            configure_input(key_input);
    
            INPUT shift_input;
            configure_input(shift_input);
            shift_input.ki.wVk = VK_SHIFT;
    
            // Type the key
            const auto virtual_key = VkKeyScanEx(character, key_board_layout);
            key_input.ki.wVk = virtual_key;
            const auto is_shift_key_required = isupper(character) || !isalnum(character);
            if(is_shift_key_required)
            {
                // Hold down the shift key
                SendInput(1, &shift_input, sizeof(INPUT));
            }
            SendInput(1, &key_input, sizeof(INPUT));
    
            // Release the key
            key_input.ki.dwFlags = KEYEVENTF_KEYUP;
            SendInput(1, &key_input, sizeof(INPUT));
    
            if(is_shift_key_required)
            {
                // Release the shift key
                shift_input.ki.dwFlags = KEYEVENTF_KEYUP;
                SendInput(1, &shift_input, sizeof(INPUT));
            }
        }
    }
    
    0 回复  |  直到 6 年前