Search code examples
c++sendinput

Struggling with SendInput()


I want to inject every possible combination of 4 digit numbers into a different program (eg notepad) by imitating the keyboard, but I cannot for the life of me figure out how to use the SendInput() function, or if there is a better alternative.

string code;
for (int digit1 = startThousand; digit1 < 10; digit1++){
    for (int digit2 = 0; digit2 < 10; digit2++){
        for (int digit3 = 0; digit3 < 10; digit3++){
            for (int digit4 = 0; digit4 < 10; digit4++){

            code = (std::to_string(digit1)+std::to_string(digit2)+std::to_string(digit3)+std::to_string(digit4));
                    //generating the 4 digit codes and outputting the final code into a single variable
            cout << code << "\n";
                    //printing the code
            SendInput(code, ???, sizeof(code))
                    //sending the code into a different program

}}}}

Solution

  • SendInput does not take in a string as parameter.

      Sendinput(cInputs, pInputs, cbSize);
    
    //cInputs = number of structures in the pInputs array..
    //pInputs = a pointer to a INPUT structure which contains info about your mouse/key event. 
    //cbSize  = the size of the structure. Tip! use sizeof();
    
    //Step 1.
    //Declare a KEYBDINPUT struct and set appropriate values to each variable.
    //See MSDN...
    //
    //Step 2.
    //Declare a INPUT structure.
    //Set type: INPUT_KEYBOARD. then set "yourinputstruct".ki equal to your keybdinput struct
    //
    //Step 3:
    //Use Sendinput function! Sendinput(1,&yourinputstruct, sizeof(yourinutstruct));
    

    for list of Virtual Keycodes see MSDN. https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes

    Here is the function that I wrote, it still may need some improvements but I think it will do what you are asking for! at least it´s working for me typing in cmd and notepad... Read on msdn or ask me to comment if it is hard to understand!

    Just pass in a Virtual keycode as first parameter and true if it is a extended key and false if it´s not!

    void KeyPress(unsigned short VK, bool ExtendedKey)
    {
        KEYBDINPUT  KeyDown = { 0 };
        KEYBDINPUT  KeyUp = { 0 };
        INPUT Input[2] = { 0 };
    
        //KeyDown...
        KeyDown.wVk = VK;
        KeyDown.wScan = 0;
        KeyDown.time = 10;
        KeyDown.dwFlags = 0;
        KeyDown.dwExtraInfo = 0;
    
        if (ExtendedKey == true)
        {
            KeyDown.wVk = VK;
            KeyDown.wScan = 0;
            KeyDown.time = 1000;
            KeyDown.dwFlags = KEYEVENTF_EXTENDEDKEY;
            KeyDown.dwExtraInfo = 0;
        }
    
        //KeyUp...
        KeyUp.wVk = VK;
        KeyUp.wScan = 0;
        KeyUp.time = 10;
        KeyUp.dwFlags = KEYEVENTF_KEYUP;
        KeyUp.dwExtraInfo = 0;
    
        if (ExtendedKey == true)
        {
            KeyUp.wVk = VK;
            KeyUp.wScan = 0;
            KeyUp.time = 10;
            KeyUp.dwFlags = KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP;
            KeyUp.dwExtraInfo = 0;
        }
        //Setup Input...
        Input[0].type = INPUT_KEYBOARD;
        Input[0].ki = KeyDown;
    
        Input[1].type = INPUT_KEYBOARD;
        Input[1].ki = KeyUp;
    
        //Click and Release!
        SendInput(1, &Input[0], sizeof(Input[0]));
        SendInput(1, &Input[1], sizeof(Input[1]));
    }