0

Solved: Check the void SendInputString( std::wstring &str) function in this answer

I am developing an executable in C++ which can be given a string as a command-line parameter and it will be typed emulating the keyboard; using keypresses.

In the format on command line:

{program name} {string to reproduce} {time between individual keypresses in ms}

This is my code:

#define WINVER 0x0500
#include <windows.h>

void chartype(char x , INPUT  &input, unsigned int pause_time){

    input.ki.wVk=VkKeyScanA(x);
    //Verifies character and allots the key to the keyboard 

    //cout<<x; //Old check code 


    Sleep(pause_time);
    
    SendInput(1,&input,sizeof(INPUT));
    //To send input  

    return;
}



int main(int argc , char** argv){
    //Initializing a GUI Keyboard Set 
    INPUT input;
    input.type=INPUT_KEYBOARD;

    //Assigning Input Variables
    string str = argv[1];
    size_t n = str.size();
    unsigned int pause_time = stoi(argv[2]);
 
    //Sending individual string characters 
    for(size_t i =0 ; i<n ; ++i){

        chartype(str[i] , input , pause_time);

    }

    //Shutting Keyboard
    input.ki.dwFlags=KEYEVENTF_KEYUP;
    SendInput(1, &input,sizeof(INPUT));
    
    return 0; 
}

However, the string is not exactly executed. For example if I input the string PASS_INPUT, it renders pass-input. Also, the output is sensitive to whether or not Caps Lock is on or not.

I assume it is happening because it only kind of emulates pressing the specific key on the keyboard, and thus assumes a default value, however I want it to present exact outputs, such as %9Xa{ to give %9Xa{ and not 59xa[ which is happening presently.

I am sorry if I could not frame the question well enough. I am new to this keyboard emulation, and am not able to exactly understand what to do.

I would like to know what modification should be made to render the string as expected.

Aniruddh Anna
  • 43
  • 1
  • 8
  • 1
    You are not zeroing out the `INPUT`, you are ignoring the `input.ki.wScan` field, and you are not simulating any `KEYEVENTF_KEYUP` events for the individual key presses. When simulating key presses for text, the `KEYEVENTF_UNICODE` flag is easier to work with than using virtual key codes. – Remy Lebeau Dec 19 '20 at 09:31
  • _I a developing a portable executable in C++_: "portable" like in "portable code" i.e. can be compiled in any standard C++ compiler/library? Probably not, because [SendInput()](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendinput) belongs to the Windows API but surely not to the C++ standard library (nor to the C standard library). – Scheff's Cat Dec 19 '20 at 09:35
  • @Scheff probably more like “portable” in that it can be run from a removable flash drive. – Remy Lebeau Dec 19 '20 at 09:38
  • 2
    See [this answer](https://stackoverflow.com/a/31307429/65863) for an example of using `KEYEVENTF_UNICODE` – Remy Lebeau Dec 19 '20 at 09:39
  • @Scheff perhaps so – Remy Lebeau Dec 19 '20 at 09:39
  • @Scheff by portable I actually meant to be able to share this executable over my other PCs which are windows based. Didn't want to compile it on every single system, since only one PC has the compiler and libraries. – Aniruddh Anna Dec 19 '20 at 12:22

1 Answers1

0

Can use the function SendInputString( std::wstring &str) from this answer

Aniruddh Anna
  • 43
  • 1
  • 8