|
|
1.1 ! root 1: #include <windows.h> ! 2: #include "console.h" ! 3: ! 4: /********************************************************************* ! 5: * FUNCTION: demoFlush(HANDLE hConOut) * ! 6: * * ! 7: * PURPOSE: demonstrate FlushConsoleInputBuffer. Slowly read from the * ! 8: * iput queue, allowing a backlog of input events to start * ! 9: * filling the queue. Flush the input queue after outputting * ! 10: * every fifth character. * ! 11: * * ! 12: * INPUT: the output console handle to write to * ! 13: *********************************************************************/ ! 14: ! 15: void demoFlush(HANDLE hConOut) ! 16: { ! 17: HANDLE hStdIn; ! 18: INPUT_RECORD InputBuffer; ! 19: DWORD dwInputEvents; ! 20: int i = 0; ! 21: BOOL bSuccess; ! 22: DWORD dwBytesWritten; ! 23: ! 24: setConTitle(__FILE__); ! 25: hStdIn = GetStdHandle(STD_INPUT_HANDLE); ! 26: PERR((int) hStdIn != -1, "GetStdHandle"); ! 27: myPuts(hConOut, "Type a number of characters quickly. I will read 5\n" ! 28: "characters from the input buffer with a Sleep() delay\n" ! 29: "which will allow it to fill with characters. After 5\n" ! 30: "characters I will flush the input buffer with\n" ! 31: "FlushConsoleInputBuffer and restart the sequence. Note\n" ! 32: "that any characters you've typed that haven't been read\n" ! 33: "yet are lost due to the flush.\n" ! 34: "Enter characters (hit ESC to return):"); ! 35: for(;;) ! 36: { ! 37: bSuccess = ReadConsoleInput(hStdIn, &InputBuffer, 1, &dwInputEvents); ! 38: PERR(bSuccess, "ReadConsoleInput"); ! 39: /* is it a key down event? */ ! 40: if (InputBuffer.EventType == KEY_EVENT && ! 41: InputBuffer.Event.KeyEvent.bKeyDown) ! 42: { ! 43: if (InputBuffer.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE) ! 44: return; ! 45: /* write the ascii character out to the console */ ! 46: bSuccess = WriteFile(hConOut, ! 47: &InputBuffer.Event.KeyEvent.uChar.AsciiChar, ! 48: 1, &dwBytesWritten, NULL); ! 49: PERR(bSuccess, "WriteFile"); ! 50: Sleep(1000); /* pause for 1s */ ! 51: i++; ! 52: if (i > 5) ! 53: { ! 54: /* flush the input buffer */ ! 55: bSuccess = FlushConsoleInputBuffer(hStdIn); ! 56: PERR(bSuccess, "FlushConsoleInputBuffer"); ! 57: i = 0; ! 58: } ! 59: } /* if */ ! 60: } /* while */ ! 61: return; ! 62: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.