Mouse input works much like keyboard input - we have a MouseState
struct that represents the state of the mouse, and we can get the current state from the static Mouse
class’s GetState()
method. You’ll also want to use the same caching strategy of a current and prior state if you want to know when a button goes down or comes up, i.e.:
MouseState currentMouseState;
MouseState priorMouseState;
public override void Update(GameTime gameTime)
{
priorMouseState = currentMouseState;
currentMouseState = Mouse.GetState();
// TODO: Add your update logic here
base.Update(gameTime);
}
However, the MouseState
struct has a different set of properties:
X
- the horizontal position of the mouse as an integer in relation to the window.Y
- the vertical position of the mouse as an integer in relation to the window.LeftButton
- aButtonState
indicating if the left button is downMiddleButton
- aButtonState
indicating if the middle button is downRightButton
- aButtonState
indicating if the right button is downScrollWheelValue
- an integer representing the cumulative scroll wheel value since the start of the gameHorizontalScrollWheelValue
- an integer representing the cumulative scroll wheel value since the start of the gameXButton1
- aButtonState
indicating if the XButton1 button is downXButton2
- aButtonState
indicating if the XButton2 is down
Note that instead of booleans
, buttons are represented by the ButtonState
enumeration. This allows the internal representation of the MouseState
buttons to be a single bitmask, making copy operations on the MouseState
much faster (and the struct itself to take up less space).
Thus, to check if the LeftButton
is down, we’d need to use:
if(currentMouseState.LeftButton == ButtonState.Pressed)
{
// left mouse button is pressed.
}
Note that not all mice have all of these possible inputs - the Horizontal scroll wheel and X buttons, especially, but many mice also lack the middle button and scroll wheel. In those cases these values will be ButtonState.Released
or false
.
The Mouse Cursor
You can set what cursor the mouse should use with the Mouse.SetCursor(MouseCursor cursor)
, and supply the cursor of your choice, i.e. MouseCursor.Crosshair
. A full list of cursors can be found in the documentation.
You can also create a cursor from a texture with MouseCursor.FromTexture2D(Texture2D texture, int originX, int originY)
. The Texture2D
is loaded with a ContentManager
, just as we did in our HelloGame example. The originX
and originY
describe where the mouse pointer is in relation to the upper-left-hand corner of the image.
You can also hide the mouse cursor by setting the Game.IsMouseVisible
property to false
.