Annotation of mstools/samples/console/readchar.c, revision 1.1

1.1     ! root        1: #include <windows.h>
        !             2: #include <string.h>
        !             3: #include <malloc.h>
        !             4: #include "console.h"
        !             5: 
        !             6: /*********************************************************************
        !             7: * FUNCTION: demoReadConChar(HANDLE hConOut)                          *
        !             8: *                                                                    *
        !             9: * PURPOSE: demonstrate ReadConsoleOutputCharacter. Read the text on  *
        !            10: *          line that the user clicks on and output it to the console *
        !            11: *                                                                    *
        !            12: * INPUT: the console output handle to write to                       *
        !            13: *********************************************************************/
        !            14: 
        !            15: void demoReadConChar(HANDLE hConOut)
        !            16: {
        !            17:   BOOL bSuccess;
        !            18:   INPUT_RECORD inputBuffer;
        !            19:   DWORD dwStdInMode;
        !            20:   HANDLE hStdIn;
        !            21:   DWORD dwInputEvents;
        !            22:   COORD coordLine; /* coordinates of where to read characters from */
        !            23:   CHAR *szLine;  /* buffer to hold the line read from the console */
        !            24:   DWORD dwCharsRead;
        !            25:   int i;
        !            26: 
        !            27:   setConTitle(__FILE__);
        !            28:   myPuts(hConOut, "Click on any line containing characters. I will use\n"
        !            29:                   "ReadConsoleOutputCharacter to read that line of text into\n"
        !            30:                   "a buffer, then print that buffer to the console at the\n"
        !            31:                   "current cursor position. Hit ESC to return.\n\n");
        !            32:   hStdIn = GetStdHandle(STD_INPUT_HANDLE);
        !            33:   PERR((int) hStdIn != -1,"GetStdHandle");
        !            34:   /* save the console mode */
        !            35:   bSuccess = GetConsoleMode(hStdIn, &dwStdInMode);
        !            36:   PERR(bSuccess, "GetConsoleMode");
        !            37:   /* enable mouse input */
        !            38:   bSuccess = SetConsoleMode(hStdIn, dwStdInMode | ENABLE_MOUSE_INPUT);
        !            39:   PERR(bSuccess, "SetConsoleMode");
        !            40:   /* allocate space for one line */
        !            41:   szLine = (char *) malloc(getConX(hConOut));
        !            42:   PERR(szLine, "malloc");
        !            43:   for(;;)
        !            44:     {
        !            45:     /* get a single input event */
        !            46:     bSuccess = ReadConsoleInput(hStdIn, &inputBuffer, 1, &dwInputEvents);
        !            47:     PERR(bSuccess, "ReadConsoleInput");
        !            48:     switch (inputBuffer.EventType)
        !            49:       {
        !            50:       case KEY_EVENT:
        !            51:         /* is it an ESC key? */
        !            52:         if (inputBuffer.Event.KeyEvent.bKeyDown &&
        !            53:             inputBuffer.Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE)
        !            54:           {
        !            55:           /* set input mode back to what it was originally and return */
        !            56:           bSuccess = SetConsoleMode(hStdIn, dwStdInMode);
        !            57:           PERR(bSuccess, "SetConsoleMode");
        !            58:           free(szLine); /* free allocated space for a text line */
        !            59:           return;
        !            60:           }
        !            61:         break;
        !            62:       case MOUSE_EVENT:
        !            63:         /* was this was a click event? Is any button down or not? */
        !            64:         if (inputBuffer.Event.MouseEvent.dwEventFlags != MOUSE_MOVED &&
        !            65:             inputBuffer.Event.MouseEvent.dwButtonState)
        !            66:           {
        !            67:           /* read the line where the mouse is, starting at column 0 */
        !            68:           coordLine.X = 0;
        !            69:           coordLine.Y = inputBuffer.Event.MouseEvent.dwMousePosition.Y;
        !            70:           bSuccess = ReadConsoleOutputCharacter(hConOut, szLine,
        !            71:               getConX(hConOut), coordLine, &dwCharsRead);
        !            72:           PERR(bSuccess, "ReadConsoleOutputCharacter");
        !            73:           /* strip trailing spaces */
        !            74:           i = getConX(hConOut) - 1;
        !            75:           szLine[i--] = 0; /* null terminate */
        !            76:           while (szLine[i] == ' ')
        !            77:             szLine[i--] = 0;
        !            78:           myPuts(hConOut, szLine);
        !            79:           }
        !            80:       } /* switch */
        !            81:     } /* while */
        !            82: }

unix.superglobalmegacorp.com

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