MouseExtended
This page is up to date for MonoGame.Extended 4.0.4
. If you find outdated information, please open an issue.
MouseExtended
is an extension of the Microsoft.Xna.Framework.Input.Mouse
class. Unlike the default MonoGame one, MouseExtended
will internally track both the mouse state of the previous frame and the mouse state of the current frame. This allows for easy comparisons between the previous and current state to check for things like single frame button presses and tracking mouse position delta between frames
MouseExtended
also contains the MouseExtended.GetState()
method which returns back a MouseStateExtended
struct. This struct provides the standard utility methods of checking if a button is down or up, but also extends this functionality with methods for checking for single frame button presses and deltas between frames.
Using MouseExtended
MouseExtended
is a static
class, so you do not need to instantiate it in the different areas you use it or pass the dependency around to different objects. However it does need to be updated once per update frame. The best place to do this is in your Game
class
protected override void Update(GameTime gameTime)
{
MouseExtended.Update();
}
MouseExtended.Update
should only be called once per update cycle in your game. This is because when calling it, it will update it's internal source for the previous state by caching the current state into the previous state before refreshing the current state. Calling this method more than once during an update cycle can cause the previous state cache to be overwritten with invalid data.
Then, just like with base MonoGame, when you need to get the state of mouse input from MouseExtended
you use the MouseExtended.GetState()
method. This returns back a new MouseStateExtended
struct
protected override void Update(GameTime gameTime)
{
MouseExtended.Update();
MouseStateExtended mouseState = MouseExtended.GetState();
}
Using MouseStateExtended
At any time, you can call MouseExtended.GetState()
to get the MouseStateExtended
struct. This struct is an extended version of the base MonoGame MouseState
that provides utility for checking mouse states between the previous and current frame.
protected override void Update(GameTime gameTime)
{
MouseExtended.Update();
MouseStateExtended mouseState = MouseExtended.GetState();
if(mouseState.WasButtonPressed(MouseButton.Left))
{
// Do something when detecting a single frame press of the left mouse button.
}
}