Thread: mouse in msvc( FAO Gape)

  1. #1
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    522

    mouse in msvc( FAO Gape)

    Ok here ya go Gape, this will display the mouse co-ordinates when you move the mouse and if a button has been pressed it will display which button. This assumes a 3 button mouse.
    Press 'q' to quit.
    Check out the MOUSE_EVENT_RECORD in your docs for all the messages you can recieve.
    Code:
    #include <stdio.h>
    #include <windows.h>
    
    void process_mouse( INPUT_RECORD *ir_ptr );
    
    int main( void )
    {
    	HANDLE hStdout, hStdin;
    	INPUT_RECORD ir;
    	UINT num;
    
    	hStdout = GetStdHandle( STD_OUTPUT_HANDLE );  // not used
    	hStdin = GetStdHandle( STD_INPUT_HANDLE );
    
    	while( ReadConsoleInput( hStdin, &ir, 1, &num ) )
    	{
    		switch( ir.EventType )
    		{
    			case MOUSE_EVENT: process_mouse( &ir );
    					  break;
    			case KEY_EVENT: if( ir.Event.KeyEvent.uChar.AsciiChar == 'q' )
    					{
    					      return 0;   // quit program
    					}
    					break;
    		}
    	}	
    	
    	return 0;
    }
    
    void process_mouse( INPUT_RECORD *ir_ptr )
    {
    	switch( ir_ptr->Event.MouseEvent.dwButtonState )
    	{
    		case FROM_LEFT_1ST_BUTTON_PRESSED : 
    				printf( "\nButton1 pressed\n" );
    				break;
    
    		case RIGHTMOST_BUTTON_PRESSED : 
    				printf( "\nButton2 pressed\n" );
    				break;
    
    		case FROM_LEFT_2ND_BUTTON_PRESSED : 
    				printf( "\nMiddle button pressed\n" );
    			        break;	
    	}
    	switch( ir_ptr->Event.MouseEvent.dwEventFlags )
    	{
    		case MOUSE_MOVED :  // print mouse co-ordinates
    			    printf( "\nMouse is at %d %d\n", 
    					     ir_ptr->Event.MouseEvent.dwMousePosition.X,
    					     ir_ptr->Event.MouseEvent.dwMousePosition.Y
    			    );
    			    break;
    	}
    }
    Last edited by C_Coder; 05-11-2002 at 11:54 AM.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  2. #2
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    Thank you!
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  3. #3
    Unregistered
    Guest

    Code doesn´t work

    I tried this code but it did not work. The compiler said it could not convert paramter 4 from 'unsigned int *' to 'unsigned long *' in the following line:
    "while( ReadConsoleInput( hStdin, &ir, 1, &num ) )"

  4. #4
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    You must declare variable num as the DWORD.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  5. #5
    Unregistered
    Guest

    Uh-huh...

    ...and this is done how? (what´s a DWORD, anyway?)

    There´s lots of stuff in that code which I do not really understand, would you mind explaining som of it?

  6. #6
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    DWORD stands for "unsigned long". What don't you understand?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  7. #7
    Unregistered
    Guest

    Pretty much...

    ...this part:

    Code:
    	HANDLE hStdout, hStdin;
    	INPUT_RECORD ir;
    	UINT num;
    
    	hStdout = GetStdHandle( STD_OUTPUT_HANDLE );  // not used
    	hStdin = GetStdHandle( STD_INPUT_HANDLE );
    Like, what does "HANDLE" and its variables(?) mean and what is "UINT num". The other stuff is functions included in one of the libraries, right? (like GetStdHandle(...))
    But what do they do?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need Help in Mouse Pointer
    By obaid in forum C++ Programming
    Replies: 3
    Last Post: 12-07-2006, 03:33 AM
  2. Problem in mouse position
    By Arangol in forum Game Programming
    Replies: 6
    Last Post: 08-08-2006, 07:07 AM
  3. Making a mouse hover button, API style
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 05-27-2004, 06:17 AM
  4. Game Design Topic #2 - Keyboard or Mouse?
    By TechWins in forum Game Programming
    Replies: 4
    Last Post: 10-08-2002, 03:34 PM
  5. Mouse in 800x600 24Bit Mode?
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 11-11-2001, 01:38 AM