5

To send a char, we can use SendInput. How can I use it to send more than one char?

I tried this code but it does not send anything:

INPUT in;
in.type=INPUT_KEYBOARD;
in.ki.wScan=0;
in.ki.time=0;
in.ki.dwExtraInfo=0;
in.ki.wVk=0x53+0x54;

SendInput(2,&in,sizeof(INPUT));

So, what is the right way?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Moe
  • 432
  • 1
  • 6
  • 21

3 Answers3

12

The first parameter of SendInput() specifies how many INPUT structures you are passing in. You are only passing in 1, but you are telling SendInput() that you are passing in 2.

You cannot specify two separate virtual keys in a single INPUT. You need to declare an array of multiple INPUTs, 2 INPUTs for each virtual key - one for the keydown event, and one for the keyup event. So, in your example, you actually need 4 INPUTs to send 2 virtual keys, as shown in @user4581301's answer.

Now, regarding KEYEVENTF_UNICODE, you don't use virtual keys with it, you use actual Unicode codepoints instead, where they are specified using UTF-16 codeunits, one per INPUT. So that means if you want to send a Unicode codepoint that requires a UTF-16 surrogate pair, you need 2 sets of down/up INPUTs, one set for the high surrogate, and one set for the low surrogate. That caveat is NOT mentioned in the SendInput() documentation, but it is implied by the fact that the vScan field is a 16bit WORD, and that KEYEVENTF_UNICODE events generate WM_CHAR messages, which passes UTF-16 surrogate codeunits as separate messages.

So, to send a string of Unicode characters using KEYEVENTF_UNICODE, you can do something like this:

#include <vector>
#include <string>

void SendInputString(const std::wstring &str)
{
    int len = str.length();
    if (len == 0) return;

    std::vector<INPUT> in(len*2);
    ZeroMemory(&in[0], in.size()*sizeof(INPUT));

    int i = 0, idx = 0;
    while (i < len)
    {
        WORD ch = (WORD) str[i++];

        if ((ch < 0xD800) || (ch > 0xDFFF))
        {
            in[idx].type = INPUT_KEYBOARD;
            in[idx].ki.wScan = ch;
            in[idx].ki.dwFlags = KEYEVENTF_UNICODE;
            ++idx;

            in[idx] = in[idx-1];
            in[idx].ki.dwFlags |= KEYEVENTF_KEYUP;
            ++idx;
        }
        else
        {
            in[idx].type = INPUT_KEYBOARD;
            in[idx].ki.wScan = ch;
            in[idx].ki.dwFlags = KEYEVENTF_UNICODE;
            ++idx;

            in[idx].type = INPUT_KEYBOARD;
            in[idx].ki.wScan = (WORD) str[i++];
            in[idx].ki.dwFlags = KEYEVENTF_UNICODE;
            ++idx;

            in[idx] = in[idx-2];
            in[idx].ki.dwFlags |= KEYEVENTF_KEYUP;
            ++idx;

            in[idx] = in[idx-2];
            in[idx].ki.dwFlags |= KEYEVENTF_KEYUP;
            ++idx;
        }
    }

    SendInput(in.size(), &in[0], sizeof(INPUT));
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • `wstring` is short for `basic_string`. A `wchar_t` is the same size as an `unsigned short` (Windows, Visual Studio). An `unsigned short` cannot hold a value >= 0x10000. If it did, your code would exhibit UB, since it would write beyond the end of the container. I believe the call to `ZeroMemory` is also not required: The container will be populated with default-(zero-)initialized structures. – IInspectable Jul 09 '15 at 10:55
  • yes, you are right about `wchar_t`. I was looking at another piece of code that accepts codepoints instead of codeunits and forgot to adjust the `if`. I have corrected it. – Remy Lebeau Jul 09 '15 at 14:55
4

If I've got you right, you want something more along the lines of this:

INPUT in[4] = {0}; // four inputs

// first input 0x53
in[0].type=INPUT_KEYBOARD;
in[0].ki.wScan=0;
in[0].ki.dwFlags=0;
in[0].ki.time=0;
in[0].ki.dwExtraInfo=0;
in[0].ki.wVk=0x53;

in[1] = in[0];
in[1].ki.dwFlags |= KEYEVENTF_KEYUP;

// second input 0x54
in[2].type=INPUT_KEYBOARD;
in[2].ki.wScan=0;
in[2].ki.dwFlags=0;
in[2].ki.time=0;
in[2].ki.dwExtraInfo=0;
in[2].ki.wVk=0x54;

in[3] = in[2];
in[3].ki.dwFlags |= KEYEVENTF_KEYUP;

SendInput(4,in,sizeof(INPUT));

Probably want to wrap the grunt work setting up the INPUT structure into a function to reduce duplication.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • that dosnt work, even if it works, that would be so hard to send a variable string – Moe Jul 09 '15 at 00:19
  • @Kordy, it does work, when used correctly (this answer was *partially* correct - it was sending two inputs for keydown events, but was not sending two additional inputs for the corresponding keyup events - I have corrected that). As for variable-length strings, you would simply allocate the `INPUT` array dynamically at runtime based on how many characters are actually in the string, and then populate the array with each character. And when sending strings, you are better off using the `KEYEVENTF_UNICODE` flag to send actual UTF-16 characters instead of sending virtual key codes, – Remy Lebeau Jul 09 '15 at 00:32
  • @RemyLebeau could you give me an example of using KEYEVENTF_UNICODE flag? – Moe Jul 09 '15 at 01:56
  • @Kordy: I posted an answer demonstrating `KEYEVENTF_UNICODE`. – Remy Lebeau Jul 09 '15 at 03:08
1

Other answer didn't seem to work in a DirectX game. I tested the following in TrackMania Nations and it worked correctly: (notepad etc work as well)

#define _WIN32_WINNT 0x0500
#include <windows.h>
#include <string>

void press_some_keys (std::string str) {
    INPUT input[2 * (int)str.size()] = {0};

    int cnt = 0;
    for (char ch: str) {
        input[cnt].type = INPUT_KEYBOARD;
        input[cnt].ki.dwFlags = KEYEVENTF_SCANCODE;
        input[cnt].ki.wScan = MapVirtualKey(LOBYTE(VkKeyScan(ch)), 0);
        cnt++;

        input[cnt] = input[cnt - 1];
        cnt++;
    }

    SendInput(2 * (int)str.size(), input, sizeof(INPUT));
    Sleep(10);

    for (cnt = 1; cnt < 2 * (int)str.size(); cnt += 2)
        input[cnt].ki.dwFlags |= KEYEVENTF_KEYUP;

    SendInput(2 * (int)str.size(), input, sizeof(INPUT));
}

I only tested lowercase alphabet characters (no arrows or others), but it was enough for me.

It was a pain to get it working since the closest thing I found was a single tap function in a Youtube video :-)

catalyst
  • 25
  • 1
  • 4