-int data_available()
-{
- static HANDLE inh = NULL;
- static bool usePipe;
- INPUT_RECORD rec[256];
- DWORD dw, recCnt;
-
- if (!inh)
- {
- inh = GetStdHandle(STD_INPUT_HANDLE);
- usePipe = !GetConsoleMode(inh, &dw);
- if (!usePipe)
- {
- SetConsoleMode(inh, dw & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
- FlushConsoleInputBuffer(inh);
- }
- }
-
- // If we're running under XBoard then we can't use PeekConsoleInput() as
- // the input commands are sent to us directly over the internal pipe.
- if (usePipe)
- return PeekNamedPipe(inh, NULL, 0, NULL, &dw, NULL) ? dw : 1;
-
- // Count the number of unread input records, including keyboard,
- // mouse, and window-resizing input records.
- GetNumberOfConsoleInputEvents(inh, &dw);
-
- // Read data from console without removing it from the buffer
- if (dw <= 0 || !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;