From: Marco Costalba Date: Thu, 18 Sep 2008 10:46:48 +0000 (+0100) Subject: Ignore non keyboard events in Bioskey() X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=95ce27f9262b63ce8eb611965e5cbc16bae815ad Ignore non keyboard events in Bioskey() Filter out mouse and windows type events. This fix an issue where Glaurung hangs in console mode under Windows. To reproduce simply open a console under Windows (cmd.exe), run "glaurung.exe bench 50 1", this starts benchmarking. Then hide the windows and show again or clik the mouse somewhere on the window, this hangs the benchmark because Boiskey() returns true and poll() calls std::getline() that hangs waiting for user pressing a return key. Signed-off-by: Marco Costalba --- diff --git a/src/misc.cpp b/src/misc.cpp index ecd6a499..cf68055c 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -178,8 +178,24 @@ int Bioskey() return 1; return dw; } else { + // Count the number of unread input records, including keyboard, + // mouse, and window-resizing input records. GetNumberOfConsoleInputEvents(inh, &dw); - return dw <= 1 ? 0 : dw; + if (dw <= 0) + return 0; + + // Read data from console without removing it from the buffer + INPUT_RECORD rec[256]; + DWORD recCnt; + if (!PeekConsoleInput(inh, rec, Min(dw, 256), &recCnt)) + return 0; + + // Search for at least one keyboard event + for (DWORD i = 0; i < recCnt; i++) + if (rec[i].EventType == KEY_EVENT) + return 1; + + return 0; } } #endif