SendInput() Keyboard letters C/C++

前端 未结 4 1434
终归单人心
终归单人心 2020-12-20 13:26

I am trying to use SendInput() to send a sentence to another application (Notepad) and then send it hitting the Enter Key.

Any code snippets?

4条回答
  •  不思量自难忘°
    2020-12-20 13:44

    I made a modification after reading @Nathan's code, this reference and combined with @jave.web's suggestion. This code can be used to input characters (both upper-case and lower-case).

    #define WINVER 0x0500
    #include
    void pressKeyB(char mK)
    {
        HKL kbl = GetKeyboardLayout(0);
        INPUT ip;
        ip.type = INPUT_KEYBOARD;
        ip.ki.time = 0;
        ip.ki.dwFlags = KEYEVENTF_UNICODE;
        if ((int)mK<65 && (int)mK>90) //for lowercase
        {
            ip.ki.wScan = 0;
            ip.ki.wVk = VkKeyScanEx( mK, kbl );
        }
        else //for uppercase
        {
            ip.ki.wScan = mK;
            ip.ki.wVk = 0;
    
        }
        ip.ki.dwExtraInfo = 0;
        SendInput(1, &ip, sizeof(INPUT));
    }
    

    Below is the function for pressing Return key:

        void pressEnter()
    {
        INPUT ip;
        ip.type = INPUT_KEYBOARD;
        ip.ki.time = 0;
        ip.ki.dwFlags = KEYEVENTF_UNICODE;
        ip.ki.wScan = VK_RETURN; //VK_RETURN is the code of Return key
        ip.ki.wVk = 0;
    
        ip.ki.dwExtraInfo = 0;
        SendInput(1, &ip, sizeof(INPUT));
    
    }
    

提交回复
热议问题