Mouse Input

This is a discussion on Mouse Input within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to make a game where your unit will move to where your mouse is on the ...

  1. #1
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227

    Mouse Input

    Hi,
    I am trying to make a game where your unit will move to where your mouse is on the screen. I read the MSDN page about console mouse input and I have this code so far:
    Code:
    ENABLE_MOUSE_INPUT;
    INPUT_RECORD MOUSE_EVENT_RECORD;
    COORD dwWritePosition;
    What I wanted to know was, isnt the COORD statement supposed to record the coordinates of the mouse, and if it does how do you retrieve the coordinates?
    Thanks
    Keyboard Not Found! Press any key to continue. . .

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Look in to the SetConsoleMode function at msdn.
    Then check out INPUT_RECORD and that should give you a good start on using everything.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    just pass an INPUT_RECORD variable into the ReadConsoleInput() function - if the EventType == KEY_EVENT, the mouse position will be in the INPUT_RECORD::Event.MouseEvent.dwMousePosition COORD.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  4. #4
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    err...umm....could i get a quick example?
    Keyboard Not Found! Press any key to continue. . .

  5. #5
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    The coordinates are stored in the INPUT_RECORD structure.
    Code:
     
    INPUT_RECORD myinput_record;
    DWORD num;
     
    ReadConsoleInput(hStdin,&myinput_record,1,&num);
     
    myinput_record.Event.MouseEvent.dwMousePosition.X
    myinput_record.Event.MouseEvent.dwMousePosition.Y
    Here is a short example
    Code:
    #include <windows.h>
    #include <iostream>
    using namespace std;
     
    int main(void)
    {
    HANDLE hStdin,hStdout;
    hStdin=GetStdHandle(STD_INPUT_HANDLE);
    hStdout=GetStdHandle(STD_OUTPUT_HANDLE);
    DWORD num;
    INPUT_RECORD inputrecord;
    COORD location;
     
    location.X=0;
    location.Y=0;
     
    while(1)
    {
     
      ReadConsoleInput(hStdin,&inputrecord,1,&num);
     
    	switch(inputrecord.EventType)
    	{
    	case MOUSE_EVENT: 
     
    	  SetConsoleCursorPosition(hStdout,location);
    	  cout<<"mouse_position ";
    	  cout<<inputrecord.Event.MouseEvent.dwMousePosition.X;
    	  cout<<" "<<inputrecord.Event.MouseEvent.dwMousePosition.Y<<"		   \n";
     
    	  break;
    	}
    }
     
    return 0;
    }
    Last edited by manofsteel972; 10-17-2004 at 04:29 AM. Reason: clean up so it doesn't scroll
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  6. #6
    Arggggh DeepFyre's Avatar
    Join Date
    Sep 2004
    Posts
    227
    Thanks a buttload man!!!
    Keyboard Not Found! Press any key to continue. . .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input class project (again)
    By Elysia in forum C++ Programming
    Replies: 41
    Last Post: 02-13-2009, 09:52 AM
  2. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 01:20 PM
  3. Mouse Input
    By bumfluff in forum C++ Programming
    Replies: 7
    Last Post: 11-11-2005, 12:23 AM
  4. Making a mouse hover button, API style
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 05-27-2004, 06:17 AM
  5. Win32 Console App- Mouse input
    By Trauts in forum Windows Programming
    Replies: 2
    Last Post: 04-29-2003, 11:32 AM

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