Results 1 to 2 of 2

Thread: Mouse Pointer in Console Window

  1. #1
    Join Date
    Mar 2003
    Posts
    1,700
    Rep Power
    0

    Default Mouse Pointer in Console Window

    Ok, no one had an answer to the last question, so I had to research it m'self. Here's the code to get the job done:

    Code:
    #include <iostream>
    #include <conio.h>
    #include <windows.h>
    
    void process_mouse( INPUT_RECORD *ir_ptr );
    void process_keyboard( INPUT_RECORD *ir_ptr );
    
    void main()
    {
       HANDLE hStdin;
       INPUT_RECORD ir;
       ULONG num;
       int kbc=0;
    
       textbackground(0);
       textcolor(15);
       gotoxy(1, 25);
    
       cputs("Press ESC to exit");
       hStdin = GetStdHandle( STD_INPUT_HANDLE );
       while( ReadConsoleInput( hStdin, &ir, 1, &num ) ){
          switch( ir.EventType ){
             case MOUSE_EVENT:
                process_mouse( &ir );
                break;
    
             case KEY_EVENT:
                kbc++;
                if (kbc == 2){
                   process_keyboard( &ir );
                       kbc = 0;
                       }
                    break;
             }
          }
    }
    
    void process_mouse( INPUT_RECORD *ir_ptr )
    {
       int x = ir_ptr->Event.MouseEvent.dwMousePosition.X;
       int y = ir_ptr->Event.MouseEvent.dwMousePosition.Y;
       textbackground(0);
       textcolor(15);                         
       gotoxy(1, 22);
       switch( ir_ptr->Event.MouseEvent.dwButtonState ){
          case FROM_LEFT_1ST_BUTTON_PRESSED :
             cprintf( "Button1 clicked" );
             break;
          case RIGHTMOST_BUTTON_PRESSED :
             cprintf( "Button2 clicked" );
             break;
          case FROM_LEFT_2ND_BUTTON_PRESSED :
             cprintf( "Middle button clicked" );
          }
       switch( ir_ptr->Event.MouseEvent.dwEventFlags ){
          case MOUSE_MOVED : 
             gotoxy(1, 23);
             cprintf( "Mouse X position: %d ", x);  
             gotoxy(1, 24);
             cprintf( "Mouse Y position: %d ", y);
          }
    }
    
    void process_keyboard( INPUT_RECORD *ir_ptr )
    {
       WORD kb;
       textbackground(0);
       textcolor(15);
       kb = ir_ptr->Event.KeyEvent.wVirtualKeyCode;
       if (kb != VK_ESCAPE){
          gotoxy(1, 21);
          cprintf("%c was pressed", ir_ptr->Event.KeyEvent.uChar);
          }
       else
          exit(1);
    }
    The way in which the program is structured, you can input to the console window using either mouse or keyboard input simultaneously. This is perfect for GUIs written using the mouse pointer. A list of Virtual Key Codes should be available in your compiler's documentation that you can use to trap keyboard input.

  2. #2
    Join Date
    Jul 2002
    Posts
    818
    Rep Power
    0

    Default Re:Mouse Pointer in Console Window

    Helpful hints like this deserve to be stickied....even for a littlw while.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •