From 4aeb7afd6a7941b8a40838e3740ab9ed7c16844c Mon Sep 17 00:00:00 2001 From: Denisa Date: Thu, 10 Jul 2025 17:22:41 +0200 Subject: [PATCH 1/3] Switch between getchar() and _getch() in windows when input is injected through stdin Signed-off-by: Denisa --- cpp_utils/src/cpp/event/StdinEventHandler.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/cpp_utils/src/cpp/event/StdinEventHandler.cpp b/cpp_utils/src/cpp/event/StdinEventHandler.cpp index e3d9d6a..ff888f2 100644 --- a/cpp_utils/src/cpp/event/StdinEventHandler.cpp +++ b/cpp_utils/src/cpp/event/StdinEventHandler.cpp @@ -255,13 +255,23 @@ void StdinEventHandler::stdin_listener_thread_routine_() noexcept { std::string read_str; size_t cursor_index = 0; - + bool use_getchar = GetFileType(GetStdHandle(STD_INPUT_HANDLE)) == FILE_TYPE_PIPE; while (true) { #if defined(_WIN32) || defined(_WIN64) int c; - c = _getch(); // Read first character + // Read first character + // Use getchar() for pipes to avoid blocking + // Use _getch() for console input to avoid echoing characters + if (use_getchar) + { + c = getchar(); + } + else + { + c = _getch(); + } if (c == 0 || c == 224) // Special key prefix for arrow keys on Windows { c = _getch(); // Get next character to determine arrow key From a7c6cee032ef8cd5ca994df5909dcc23f74b148c Mon Sep 17 00:00:00 2001 From: Denisa Date: Thu, 10 Jul 2025 17:32:51 +0200 Subject: [PATCH 2/3] Fix compilation flag for windows Signed-off-by: Denisa --- cpp_utils/src/cpp/event/StdinEventHandler.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cpp_utils/src/cpp/event/StdinEventHandler.cpp b/cpp_utils/src/cpp/event/StdinEventHandler.cpp index ff888f2..95ecf73 100644 --- a/cpp_utils/src/cpp/event/StdinEventHandler.cpp +++ b/cpp_utils/src/cpp/event/StdinEventHandler.cpp @@ -255,7 +255,9 @@ void StdinEventHandler::stdin_listener_thread_routine_() noexcept { std::string read_str; size_t cursor_index = 0; +#if defined(_WIN32) || defined(_WIN64) bool use_getchar = GetFileType(GetStdHandle(STD_INPUT_HANDLE)) == FILE_TYPE_PIPE; +#endif // if defined(_WIN32) || defined(_WIN64) while (true) { From 8d9afb9f3e45cf83a210c0240616c5e8194f5c6c Mon Sep 17 00:00:00 2001 From: Denisa Date: Fri, 11 Jul 2025 10:41:04 +0200 Subject: [PATCH 3/3] Fix uncrustify Signed-off-by: Denisa --- cpp_utils/src/cpp/event/StdinEventHandler.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp_utils/src/cpp/event/StdinEventHandler.cpp b/cpp_utils/src/cpp/event/StdinEventHandler.cpp index 95ecf73..1a7e304 100644 --- a/cpp_utils/src/cpp/event/StdinEventHandler.cpp +++ b/cpp_utils/src/cpp/event/StdinEventHandler.cpp @@ -268,7 +268,7 @@ void StdinEventHandler::stdin_listener_thread_routine_() noexcept // Use _getch() for console input to avoid echoing characters if (use_getchar) { - c = getchar(); + c = getchar(); } else {