Log in Page Discussion History Go to the site toolbox

XNA Tutorials/Mouse Input

From BluWiki



If you are developing a Windows application with XNA, you'd like your application to receive mouse input. This brief tutorial adds mouse input to the example shown in the MSDN tutorial Your First Game: Microsoft XNA Game Studio in 2D.

This tutorial was created by Frank McCown as part of the COMP 475 XNA Tutorials.

Display the Mouse Pointer

The default behavior of an XNA application is to hide the mouse pointer when it is placed on the window. To make the mouse pointer visible, change the IsMouseVisible property of the Game class to true:

protected override void Initialize()
   // Make mouse visible
   this.IsMouseVisible = true;


The Mouse class and MouseState struct

XNA provides a Mouse class that is defined in the Microsoft.Xna.Framework.Input namespace. The Mouse class has a static method called GetState which returns a MouseState struct; this struct will tell you if a mouse button is pressed or released and the (x,y) location of the mouse pointer.

The following table lists the important properties of the MouseState struct:

Property Type Description
LeftButton ButtonState Returns the state of the left mouse button.
MiddleButton ButtonState Returns the state of the middle mouse button.
RightButton ButtonState Returns the state of the right mouse button.
ScrollWheelValue int Returns the total accumulated movement of the scroll wheel since the game started.
X int Returns the horizontal position of the mouse in relation to the upper-left corner of the window.
Y int Returns the vertical position of the mouse in relation to the upper-left corner of the window.


The following example shows how to make the animated sprite move to the current location of the mouse pointer when the left mouse button is pressed. The horizontal direction of the sprite changes direction when the right mouse button is clicked.

First add the class-level variables:

MouseState mouseStateCurrent, mouseStatePrevious;

Then modify the Update function:

protected override void Update(GameTime gameTime)
   // Allows the game to exit
   if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
   mouseStateCurrent = Mouse.GetState();

   // Move the sprite to the current mouse position when the left button is pressed
   if (mouseStateCurrent.LeftButton == ButtonState.Pressed)
      spritePosition.X = mouseStateCurrent.X;
      spritePosition.Y = mouseStateCurrent.Y;

   // Change the horizontal direction of the sprite when the right mouse button is clicked
   if (mouseStateCurrent.RightButton == ButtonState.Pressed &&
       mouseStatePrevious.RightButton == ButtonState.Released)
       spriteSpeed.X *= -1; 

   mouseStatePrevious = mouseStateCurrent;

   // Move the sprite around.


The mouseStateCurrent and mouseStatePrevious variables are used to determine if a complete right-click has been made. If you only check the Pressed property (as is done for the left mouse button), it will be set to true through multiple iterations of the Update function since Update is being called 60 times a second. Because we only want to change the sprite's speed when the right-mouse press is first detected, the mouse button must be released in the previous call to Update.

Since the left-mouse press is not checking for the previous button state, it will continue to change the sprite's (x,y) location as the mouse button is held down.

A screen shot of this program is shown below. The user has clicked the left mouse button, so the image is drawn at the mouse pointer location.

Xna mouse screenshot small.png


Site Toolbox:

Personal tools
GNU Free Documentation License 1.2
This page was last modified on 3 September 2009, at 19:38.
Disclaimers - About BluWiki