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

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

unix.superglobalmegacorp.com

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