MSVC and console event

This is a discussion on MSVC and console event within the Windows Programming forums, part of the Platform Specific Boards category; Code: //This file is intended to use all Win32 API console functions for learning purposes //Consoles are used to build ...

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231

    Exclamation MSVC and console event

    Code:
    //This file is intended to use all Win32 API console functions for learning purposes
    //Consoles are used to build character mode applications
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <stdlib.h>
    
    int main()
    {
          SetConsoleTitle("Power Console");
          //Getting Handles
          HANDLE hIn;
        HANDLE hOut;
        HANDLE hError;
          AllocConsole();
        hIn = GetStdHandle(STD_INPUT_HANDLE);
        hOut = GetStdHandle(STD_OUTPUT_HANDLE);
        hError = GetStdHandle(STD_ERROR_HANDLE);
    
    
          //Writing to console
          DWORD n;
          WriteConsole(hOut, "Hello World!\n",13, &n, NULL);
    
          //Changing cursor position
          COORD Position;
          for(int i=5; i<14; i+=2)
        {
                Sleep(400);
            Position.X = i;
            Position.Y = i;
            SetConsoleCursorPosition(hOut, Position);
            WriteConsole(hOut, "Here",4, &n, NULL);
        }
    
          //This function writes a defined number of one char on screen
          Position.X = 5;
        Position.Y = 5;
          FillConsoleOutputCharacter(hOut, 'X', 15, Position, &n);
    
          //Now we read from screen buffer
          CHAR read[16];
          Position.X = 0;
        Position.Y = 0;
          ReadConsoleOutputCharacter(hOut, read, 13, Position, &n);
          //And its sister function to write chars
          Position.X = 0;
        Position.Y = 10;
          WriteConsoleOutputCharacter(hOut, read, n, Position, &n);
    
          //Now lets play with the cursor
     //First we need the struct
          CONSOLE_CURSOR_INFO ConCurInf;
          GetConsoleCursorInfo(hOut, &ConCurInf);
          DWORD initialSize = ConCurInf.dwSize;
          for(int i= 0; i<100;i+=4)
          {
                ConCurInf.dwSize = i;
                SetConsoleCursorInfo(hOut, &ConCurInf);
                Sleep(150);
          }
          ConCurInf.dwSize = initialSize; //Back to normal size
          SetConsoleCursorInfo(hOut, &ConCurInf);
    
          //ChangingColor
           SetConsoleTextAttribute(hOut,
                                FOREGROUND_GREEN | 
                                FOREGROUND_RED | FOREGROUND_INTENSITY | BACKGROUND_BLUE);
           WriteConsole(hOut, "Hello World!\n",13, &n, NULL);
           //Now we want to know the color which we wrote with
     //First we need the struct
           CONSOLE_SCREEN_BUFFER_INFO ConScrBuffInf;
           GetConsoleScreenBufferInfo(hOut, &ConScrBuffInf);
           Position = ConScrBuffInf.dwCursorPosition;
    
           Position.Y -=1;
           Position.X +=10;
           WORD attrib;
           ReadConsoleOutputAttribute(hOut, &attrib, 1 , Position, &n);
    
           Sleep(1000);
           FillConsoleOutputAttribute(hOut, FOREGROUND_BLUE| FOREGROUND_INTENSITY,70, Position, &n);
    
           //Now Events
           
           INPUT_RECORD InRec;
           bool Continue = TRUE;
           int KeyEvents = 0;
         int MouseEvents = 0;
         COORD KeyWhere;
         COORD MouseWhere;
         COORD EndWhere;
    
          Position.Y =20;
          Position.X =0;
          SetConsoleCursorPosition(hOut, Position);
          WriteConsole(hOut, "Key Events   : \n", 16,&n,NULL);
          WriteConsole(hOut, "Mouse Events : ", 15,&n,NULL);
       
    
          KeyWhere.X = 15;
          KeyWhere.Y = 20;
          MouseWhere.X = 15;
          MouseWhere.Y = 21;
          EndWhere.X = 0;
          EndWhere.Y = 22;
          read[0] = '\0';
           while (Continue)
         {
            ReadConsoleInput(hIn, &InRec, 1, &n);
    
            switch (InRec.EventType)
            {
            case KEY_EVENT:
                ++KeyEvents;
                SetConsoleCursorPosition(hOut, KeyWhere);
                      _itoa(KeyEvents, read,10);
                WriteConsole(hOut, read, strlen(read), &n, NULL);
                if (InRec.Event.KeyEvent.uChar.AsciiChar == 'x')
                {
                    SetConsoleCursorPosition(hOut, EndWhere);
                    WriteConsole(hOut, "Exiting...\n", 11, &n, NULL);
                    Continue = FALSE;
                }
                break;
    
            case MOUSE_EVENT:
                ++MouseEvents;
                SetConsoleCursorPosition(hOut, MouseWhere);
                      _itoa(MouseEvents, read, 10);
                WriteConsole(hOut, read, strlen(read), &n, NULL);
                break;
            }
        }
    
          //Another evant handling
          Position.Y =25;
          Position.X =0;
          SetConsoleCursorPosition(hOut, Position);
           WriteConsole(hOut, "Key Events   : ", 15,&n,NULL);
           unsigned char HoldKey;
          COORD LoopWhere;
          int LoopCount = 0;
           DWORD EventCount;
    
    
        KeyWhere.X = 15;
        KeyWhere.Y = 25;
        LoopWhere.X = 0;
        LoopWhere.Y = 26;
        EndWhere.X = 0;
        EndWhere.Y = 27;
          Continue = true;
          KeyEvents = 0;
        while (Continue)
        {
            SetConsoleCursorPosition(hOut, LoopWhere);
            _itoa(LoopCount++, read,10);
                WriteConsole(hOut, read, strlen(read), &n, NULL);
                WriteConsole(hOut, "      ", 6, &n, NULL);
            Sleep(30); // To slow it down!!
    
            GetNumberOfConsoleInputEvents(hIn, &EventCount);
            while (EventCount > 0)
            {
                ReadConsoleInput(hIn, &InRec, 1, &n);
                if (InRec.EventType == KEY_EVENT)
                {
                    if (InRec.Event.KeyEvent.bKeyDown)
                    {
                        HoldKey = InRec.Event.KeyEvent.uChar.AsciiChar;
                    }
                    else
                    {
                        ++KeyEvents;
                        SetConsoleCursorPosition(hOut, KeyWhere);
                                  _itoa(KeyEvents, read, 10);
                        WriteConsole(hOut,read, strlen(read), &n, NULL);
    
                        LoopCount = 0;
    
                        if (HoldKey == 'x')
                        {
                            if (InRec.Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED)
                            {
                                SetConsoleCursorPosition(hOut, EndWhere);
                                WriteConsole(hOut,"Exiting...", 10, &n, NULL);
                                Continue = FALSE;
                            }
                        }
                    }
                }
    
                GetNumberOfConsoleInputEvents(hIn, &EventCount);
            }
        }
    
    
    
    
    
          
          return 0;
    }
    I started learning Win32 console API. In the first event handler loop of this program it detects mouse events. In Visual Studio when I run in debug mode it detects mouse events correctly but when I start it without debugging it doesnt detect it. when I execute its file externaly it is alright.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Just to help someone else.
    It is because when using Ctrl+F5 in MSVC, it runs your application as a child process of cmd.exe. Because of the way cmd opens console, console doesn't go in ENABLE_MOUSE_INPUT mode. You should set that mode manually.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memcpy working strangely in msvc 2005
    By *DEAD* in forum C++ Programming
    Replies: 1
    Last Post: 06-15-2007, 09:50 AM
  2. MSVC Console app - two enters?
    By LuckY in forum Windows Programming
    Replies: 4
    Last Post: 12-30-2003, 01:13 PM
  3. Resizing a Borland Builder 4.0 Console
    By dalek in forum C++ Programming
    Replies: 5
    Last Post: 07-19-2003, 06:20 AM
  4. console text color change in MSVC?
    By revelation437 in forum C++ Programming
    Replies: 1
    Last Post: 05-16-2003, 01:46 PM
  5. Console text...
    By Engel32 in forum C++ Programming
    Replies: 1
    Last Post: 09-16-2001, 08:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21