O'Reilly logo

Learning XNA 3.0 by Aaron Reed

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mouse Input

XNA provides a Mouse class to interact with the mouse that behaves very similarly to the Keyboard class. The Mouse class also has a GetState method that you can use to get data from the mouse in the form of a MouseState struct. The Mouse class also has another method worthy of note: void SetPosition(int x, int y). This method will—you guessed it—allow you to set the position of the mouse. This position is relative to the upper-left corner of the game window.

The MouseState struct has several properties that will help you understand what is happening with the mouse at the particular moment in time when you called GetState. These properties are detailed in Table 3-2.

Table 3-2. 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. To find out how much the scroll wheel has moved, compare this value to the previous frame's scroll wheel value.

X

int

Returns the value of the horizontal position of the mouse in relation to the upper-left corner of the game window. If the mouse is to the left of the game window, the value is negative. If the mouse is to the right of the game window, the value is greater than the width ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required