I find it quite handy that I can use SendInput to mix text and controls into single string and send it to any application like
"\nHello\nWorld"
would end up with sequence of
hit {Enter}
type "Hello"
hit {enter}
type "World"
In any chat application (for example).
I have the following code
void GenerateKey(WORD vk)
{
INPUT Input;
ZeroMemory(&Input, sizeof(Input));
Input.type = INPUT_KEYBOARD;
Input.ki.time = 0;
Input.ki.dwFlags = KEYEVENTF_EXTENDEDKEY | KEYEVENTF_UNICODE;
Input.ki.wVk = vk;
SendInput(1, &Input, sizeof(INPUT));
return;
}
void SendKeys(const LPCWSTR format, ...)
{
va_list args;
va_start(args, format);
WCHAR buffer[256];
vswprintf_s(buffer, format, args);
va_end(args);
BlockInput(true);
for (unsigned int i = 0; i<wcslen(buffer); ++i)
GenerateKey((WORD)VkKeyScan(buffer[i]));
BlockInput(false);
}
It works pretty fine. But the problem is, it depends on the currently used layout of the keyboard.
I have also tried to use LoadKeyboardLayout, but the following has no effect at all
BlockInput(true);
HKL keyboard_layout = LoadKeyboardLayout("00000409", KLF_ACTIVATE);
for (unsigned int i = 0; i<wcslen(buffer); ++i)
GenerateKey((WORD)VkKeyScanExW(buffer[i], keyboard_layout));
BlockInput(false);
I also welcome any ideas to achieve the goal.
SendMessage isn't an option, cause it would require me to get a handle to the window and also to the text box.