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

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

unix.superglobalmegacorp.com

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