Skip to content

Commit 3bfcc19

Browse files
authored
Switch between getchar() and _getch() in windows when input is injected (#139)
* Switch between getchar() and _getch() in windows when input is injected through stdin Signed-off-by: Denisa <denisa.alexandru@alumnos.upm.es> * Fix compilation flag for windows Signed-off-by: Denisa <denisa.alexandru@alumnos.upm.es> * Fix uncrustify Signed-off-by: Denisa <denisa@eprosima.com> --------- Signed-off-by: Denisa <denisa.alexandru@alumnos.upm.es> Signed-off-by: Denisa <denisa@eprosima.com>
1 parent 89c1650 commit 3bfcc19

1 file changed

Lines changed: 14 additions & 2 deletions

File tree

cpp_utils/src/cpp/event/StdinEventHandler.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,13 +255,25 @@ void StdinEventHandler::stdin_listener_thread_routine_() noexcept
255255
{
256256
std::string read_str;
257257
size_t cursor_index = 0;
258-
258+
#if defined(_WIN32) || defined(_WIN64)
259+
bool use_getchar = GetFileType(GetStdHandle(STD_INPUT_HANDLE)) == FILE_TYPE_PIPE;
260+
#endif // if defined(_WIN32) || defined(_WIN64)
259261
while (true)
260262
{
261263

262264
#if defined(_WIN32) || defined(_WIN64)
263265
int c;
264-
c = _getch(); // Read first character
266+
// Read first character
267+
// Use getchar() for pipes to avoid blocking
268+
// Use _getch() for console input to avoid echoing characters
269+
if (use_getchar)
270+
{
271+
c = getchar();
272+
}
273+
else
274+
{
275+
c = _getch();
276+
}
265277
if (c == 0 || c == 224) // Special key prefix for arrow keys on Windows
266278
{
267279
c = _getch(); // Get next character to determine arrow key

0 commit comments

Comments
 (0)