|
|
1.1 root 1: #include <windows.h>
2: #include <stdio.h>
3: #include <string.h>
4: #include "console.h"
5:
1.1.1.2 ! root 6: /* Microsoft Developer Support
! 7: Copyright (c) 1992 Microsoft Corporation */
! 8:
1.1 root 9: /* maximum number of input queue events to peek at */
10: #define INPUT_RECS 256
11:
12: /* array of records to store peeked events from the input queue */
13: INPUT_RECORD aInputBuffer[INPUT_RECS];
14:
15: /*********************************************************************
16: * FUNCTION: demoGetNumEvents(HANDLE hConOut) *
17: * *
18: * PURPOSE: demonstrate GetNumberOfConsoleInputEvents, *
19: * PeekConsoleInput, and ReadConsoleInput. Delay the *
20: * processing of console input to start filling the input *
21: * queue. The number of console events in the input queue *
22: * will be updated in the status line at the top of the *
23: * console. Peek the unread characters for an ESC and return *
24: * when one is found *
25: * *
26: * INPUT: console output handle to write to *
27: *********************************************************************/
28:
29: void demoGetNumEvents(HANDLE hConOut)
30: {
31: BOOL bSuccess;
32: DWORD dwNumEvents; /* number of events in the input queue */
33: DWORD dwStdInMode; /* save the input mode here */
34: HANDLE hStdIn;
35: DWORD dwInputEvents; /* number of events read from the queue */
36: CHAR bOutBuf[256], szTemp[256];
37: /* indexes to latest unread event checked for ESC char */
38: DWORD iEvent, iPrevEvent;
39: DWORD dwEventsPeeked; /* number of events peeked at */
40: unsigned i;
41: DWORD dwCharsWritten;
42:
43: setConTitle(__FILE__);
44: hStdIn = GetStdHandle(STD_INPUT_HANDLE);
1.1.1.2 ! root 45: PERR(hStdIn != INVALID_HANDLE_VALUE, "GetStdHandle");
1.1 root 46: myPuts(hConOut, "\nLet's display a running count of events waiting in the\n"
47: "console input queue by using the GetNumberOfConsoleInputEvents\n"
48: "API. I will enable mouse input in this console; try\n"
49: "generating a lot of mouse and keyboard events. I will\n"
50: "read them from the input queue using ReadConsoleInput\n"
51: "with .5 second delays between reads. Note the event\n"
52: "count and event information on the top line.\n\n"
53: "Hit ESC at any time to return. We will use\n"
54: "PeekConsoleInput to monitor the unread input queue\n"
55: "contents for an ESC character. When this character\n"
56: "is detected, we will flush the input queue and return\n"
57: "immediately. Note that for simplification of this\n"
58: "demo only the first 256 unread events will be scanned\n"
59: "to find an ESC keystroke.\n\n");
60:
61: bSuccess = GetConsoleMode(hStdIn, &dwStdInMode);
62: PERR(bSuccess, "GetConsoleMode");
63: /* when turning off ENABLE_LINE_INPUT, you MUST also turn off */
64: /* ENABLE_ECHO_INPUT. */
65: bSuccess = SetConsoleMode(hStdIn, (dwStdInMode & ~(ENABLE_LINE_INPUT |
66: ENABLE_ECHO_INPUT)) | ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT);
67: PERR(bSuccess, "SetConsoleMode");
68: iEvent = 0; /* index to last event peeked in the input queue */
69: for(;;)
70: {
71: Sleep(500);
72: bSuccess = GetNumberOfConsoleInputEvents(hStdIn, &dwNumEvents);
73: PERR(bSuccess, "GetNumberOfConsoleInputEvents");
74: sprintf(bOutBuf, "input queue events: %d", dwNumEvents);
75: if (!dwNumEvents)
76: /* put a status line on the first line */
77: putStatusLine(hConOut, bOutBuf);
78: else
79: {
80: /* save the previous index we've peeked at */
81: iPrevEvent = iEvent;
82: /* peek at the console input queue. Don't peek more than what will */
83: /* fit in the buffer */
84: bSuccess = PeekConsoleInput(hStdIn, aInputBuffer, min(dwNumEvents,
85: INPUT_RECS), &dwEventsPeeked);
86: PERR(bSuccess, "PeekConsoleInput");
87: /* set current index to the highest number event peeked at */
88: iEvent = dwEventsPeeked;
89: /* scan unread events for an ESC key */
90: for (i = iPrevEvent; i < iEvent; i++)
91: {
92: if (aInputBuffer[i].EventType == KEY_EVENT &&
93: aInputBuffer[i].Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE)
94: {
95: /* set input mode back to what it was originally */
96: bSuccess = SetConsoleMode(hStdIn, dwStdInMode);
97: PERR(bSuccess, "SetConsoleMode");
98: /* flush the input buffer and return */
99: bSuccess = FlushConsoleInputBuffer(hStdIn);
100: PERR(bSuccess, "FlushConsoleInputBuffer");
101: return;
102: }
103: } /* for */
104: } /* else */
105: bSuccess = ReadConsoleInput(hStdIn, &aInputBuffer[0], 1, &dwInputEvents);
106: PERR(bSuccess, "ReadConsoleInput");
107: /* decrement "last peeked at" index by number of records we just read */
108: iEvent -= dwInputEvents;
109: switch (aInputBuffer[0].EventType)
110: {
111: case KEY_EVENT:
112: if (aInputBuffer[0].Event.KeyEvent.bKeyDown)
113: {
114: if (aInputBuffer[0].Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE)
115: {
116: /* set input mode back to what it was originally */
117: bSuccess = SetConsoleMode(hStdIn, dwStdInMode);
118: PERR(bSuccess, "SetConsoleMode");
119: return;
120: }
121: /* put the keystroke information on the status line */
122: sprintf(szTemp, " keystroke: %c",
123: aInputBuffer[0].Event.KeyEvent.uChar.AsciiChar);
124: strcat(bOutBuf, szTemp);
125: /* put the status line on the screen */
126: putStatusLine(hConOut, bOutBuf);
127: /* output the character read from the input queue */
128: bSuccess = WriteFile(hConOut,
129: &aInputBuffer[0].Event.KeyEvent.uChar.AsciiChar, 1,
130: &dwCharsWritten, NULL);
131: PERR(bSuccess, "WriteFile");
132: }
133: break;
134: case MOUSE_EVENT:
135: sprintf(szTemp, " mouse: %s at %d, %d",
136: (aInputBuffer[0].Event.MouseEvent.dwEventFlags == MOUSE_MOVED ?
137: "moved" : "clicked"), aInputBuffer[0].Event.MouseEvent.dwMousePosition.X,
138: aInputBuffer[0].Event.MouseEvent.dwMousePosition.Y);
139: strcat(bOutBuf, szTemp);
140: putStatusLine(hConOut, bOutBuf);
141: break;
142: case WINDOW_BUFFER_SIZE_EVENT:
143: sprintf(szTemp, " window: %d, %d",
144: aInputBuffer[0].Event.WindowBufferSizeEvent.dwSize.X,
145: aInputBuffer[0].Event.WindowBufferSizeEvent.dwSize.Y);
146: strcat(bOutBuf, szTemp);
147: putStatusLine(hConOut, bOutBuf);
148: break;
149: } /* switch */
150: } /* while */
151: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.