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