Annotation of mstools/samples/console/getnumev.c, revision 1.1.1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.