Win32 Console App- Mouse input

This is a discussion on Win32 Console App- Mouse input within the Windows Programming forums, part of the Platform Specific Boards category; How would you do mouse input? And is there a way to get it to check "real time", so that ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    Win32 Console App- Mouse input

    How would you do mouse input? And is there a way to get it to check "real time", so that you can be running other things while it does that, or is that extremely complex?

    Thanks abunch!

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here's an example taken mainly from MSDN

    Code:
    #include <stdio.h>
    #include <windows.h>
    
    void MouseEventProc(MOUSE_EVENT_RECORD); 
    VOID ResizeEventProc(WINDOW_BUFFER_SIZE_RECORD); 
    VOID KeyEventProc(KEY_EVENT_RECORD); 
    VOID GetInputEvents(VOID); 
    void gotoxy(int, int);
     
    void MyErrorExit(char *s)
    {
    	printf ("Fatal: %s\n", s);
    	exit (1);
    }
    
    int main(void) 
    { 
        HANDLE hStdin; 
        DWORD cNumRead, fdwMode, fdwSaveOldMode, i; 
        INPUT_RECORD irInBuf[128]; 
     
        // Get the standard input handle. 
     
        hStdin = GetStdHandle(STD_INPUT_HANDLE); 
        if (hStdin == INVALID_HANDLE_VALUE) 
            MyErrorExit("GetStdHandle"); 
     
        // Save the current input mode, to be restored on exit. 
     
        if (! GetConsoleMode(hStdin, &fdwSaveOldMode) ) 
            MyErrorExit("GetConsoleMode"); 
     
        // Enable the window and mouse input events. 
     
        fdwMode = ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT; 
        if (! SetConsoleMode(hStdin, fdwMode) ) 
            MyErrorExit("SetConsoleMode"); 
     
        // Loop to read and handle the input events. 
     
        while (1) 
        { 
     
            // Wait for the events. 
     
            if (! ReadConsoleInput( 
                    hStdin,      // input buffer handle 
                    irInBuf,     // buffer to read into 
                    128,         // size of read buffer 
                    &cNumRead) ) // number of records read 
                MyErrorExit("ReadConsoleInput"); 
     
            // Dispatch the events to the appropriate handler. 
     
            for (i = 0; i < cNumRead; i++) 
            {
                switch(irInBuf[i].EventType) 
                { 
                    case KEY_EVENT: // keyboard input 
                        KeyEventProc(irInBuf[i].Event.KeyEvent); 
                        break; 
     
                    case MOUSE_EVENT: // mouse input 
                        MouseEventProc(irInBuf[i].Event.MouseEvent); 
                        break; 
     
                    case WINDOW_BUFFER_SIZE_EVENT: // scrn buf. resizing 
                        ResizeEventProc( 
                            irInBuf[i].Event.WindowBufferSizeEvent); 
                        break; 
     
                    case FOCUS_EVENT:  // disregard focus events 
     
                    case MENU_EVENT:   // disregard menu events 
                        break; 
     
                    default: 
                        MyErrorExit("unknown event type"); 
                        break; 
                } 
            }
        } 
     
        return 0; 
    } 
    
    VOID MouseEventProc(MOUSE_EVENT_RECORD ir)
    {
        if (ir.dwEventFlags == MOUSE_MOVED)
    		gotoxy (ir.dwMousePosition.X,ir.dwMousePosition.Y);
    }
    
    VOID ResizeEventProc(WINDOW_BUFFER_SIZE_RECORD dummy){return;};
    VOID KeyEventProc(KEY_EVENT_RECORD dummy){return;};
    VOID GetInputEvents(VOID){return;};
    
    void gotoxy(int x, int y)
    {
       COORD coord;
       coord.X = x;
       coord.Y = y;
       SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE), coord);
    }
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    Great! I'll mess around with that.

    But how can I make it check constantly without making it slow down a lot?

    Thats perfect though... the constant thing was so I could create my own "ctrl+c"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  2. MSVC Console app - two enters?
    By LuckY in forum Windows Programming
    Replies: 4
    Last Post: 12-30-2003, 02:13 PM
  3. Mouse support in console mode
    By GaPe in forum C Programming
    Replies: 1
    Last Post: 04-17-2002, 06:15 AM
  4. win32 apps compiled with GCC start console window!
    By Citrus538 in forum Windows Programming
    Replies: 5
    Last Post: 02-18-2002, 10:35 PM
  5. Mouse Input????
    By Brandon in forum Game Programming
    Replies: 1
    Last Post: 11-11-2001, 10:04 AM

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