To terminate a blocking Input from another thread, I tried to simulate a input event or more precisely a kayboard input using WriteConsoleInput function.
#include<Windows.h>
#include<conio.h>
#include<thread>
void KillBlockingIO() {
DWORD entityWritten;
INPUT_RECORD inputs;
inputs.EventType = KEY_EVENT;
inputs.Event.KeyEvent.bKeyDown = true;
inputs.Event.KeyEvent.uChar.AsciiChar = VK_RETURN;
inputs.Event.KeyEvent.wRepeatCount = 0;
inputs.Event.KeyEvent.dwControlKeyState = 0;
inputs.Event.KeyEvent.wVirtualKeyCode = 0;
inputs.Event.KeyEvent.wVirtualScanCode = 0;
// inputs.Event = { true, 0, 0, 0, VK_RETURN, 0 }; // same as above
Sleep(2000);
WriteConsoleInputA(GetStdHandle(STD_INPUT_HANDLE), &inputs, 1, &entityWritten);
}
int main()
{
std::thread t(KillBlockingIO);
char c = _getch();
printf("character recieved without typing: %c\n", c);
t.join();
}
It is working but I'm not sure, I've used WriteConsoleInput
function property because members like wVirtualKeyCode
, wVirtualScanCode
and dwControlKeyState
is set zero. No matter what value I pass, It is always going to have same result. It is also not very well Documented. I tried finding code examples but there is no such example.
what is purpose of these parameters and How to use WriteConsoleInput function properly?