1

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?

vector X
  • 89
  • 7
  • Does this help? https://learn.microsoft.com/en-us/windows/console/key-event-record-str – KungPhoo Jul 14 '22 at 05:56
  • @KungPhoo Thanks, But I've already read this documentation. It only tells about members but not in detail. – vector X Jul 14 '22 at 06:28
  • * wVirtualKeyCode - https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes * wVirtualScanCode - device-dependent value generated by the keyboard hardware * dwControlKeyState - CAPSLOCK_ON, LEFT_CTRL_PRESSED, etc It's all explained in detail, isn't it? What's the question then? – KungPhoo Jul 14 '22 at 07:06
  • "No matter what value I pass, It is always going to have same result" I want to know what difference these parameters bring. Are they Important or not? Am I missing something? – vector X Jul 14 '22 at 07:23
  • I see. Well, they are just indicators, that, together with the key-character that was passed, the Ctrl-key e.g. is also held. If the CAPSLOCK_ON is present, you will propably get a character that is already uppercase. But it allows you to pass "Ctrl+X", if I understand it correctly. However - you must pass the right values. See this answer for more details: https://stackoverflow.com/questions/39695431/ctrl-s-input-event-in-windows-console-with-readconsoleinputw – KungPhoo Jul 14 '22 at 07:46
  • A program can use [ReadConsoleInput()](https://learn.microsoft.com/en-us/windows/console/readconsoleinput) to get low-level info about pressed keys. You can see from the doc page that it gets the exact structure back that you created here. It's quite rare for an app to do this, a possible use is a game that needs to distinguish a press of the left vs the right shift key. So sure, that it works for the Enter key like this is not surprising. You can't get a warranty without describing how the program receives input. – Hans Passant Jul 14 '22 at 08:01

0 Answers0