Color legend plain property method event
MouseManager reads the mouse at every update into one MouseStatus: five buttons, the pointer and two wheels,
each with how far it moved since the update before. Game code reads the status, or listens to the manager’s events.
Names are colored by what they are: a plain property, a method or an event.
The mouse status
MouseStatus is the same object for the whole run, and its buttons are too, so a reference to either
stays good:
var mouse = MouseManager.MouseStatus; // once, or every frame: it is the same object
if (mouse.RightButton.Pressed) OpenContextMenu(mouse.Position);
The status is only ever updated by the manager. MouseStatus has a public constructor, but a status you create
stays idle and at zero.
MouseManager properties
| Property | Type | Description |
|---|---|---|
MouseStatus |
MouseStatus |
Static, read-only. The mouse as of the last update. The same object for the whole run. |
Buttons
LeftButton, MiddleButton, RightButton, XButton1 and
XButton2 are each a MouseButtonInfo, with the state and the five checks
of every button. Pressed, Down and Up are the three moments of a drag:
var mouse = MouseManager.MouseStatus;
if (mouse.LeftButton.Pressed) _dragStart = mouse.Position; // the drag starts
if (mouse.LeftButton.Down) _selection = RectangleBetween(_dragStart, mouse.Position); // it goes on
if (mouse.LeftButton.Up) SelectUnitsIn(_selection); // it ends
RectangleBetween and SelectUnitsIn stand for your own code. The middle button is usually a press on the wheel, and
the X buttons are the side buttons, back and forward on most mice.
The four IsAnyButton properties ask about all five buttons at once, such as “click anywhere to continue”:
if (MouseManager.MouseStatus.IsAnyButtonPressed) SkipCutscene();
MouseStatus button properties
| Property | Type | Description |
|---|---|---|
LeftButton |
MouseButtonInfo |
Read-only. The left button. |
MiddleButton |
MouseButtonInfo |
Read-only. The middle button, usually a press on the wheel. |
RightButton |
MouseButtonInfo |
Read-only. The right button. |
XButton1 |
MouseButtonInfo |
Read-only. The first side button, back on most mice. |
XButton2 |
MouseButtonInfo |
Read-only. The second side button, forward on most mice. |
IsAnyButtonPressed |
bool |
Read-only. Any of the five buttons went down at the last update. |
IsAnyButtonDown |
bool |
Read-only. Any of the five buttons is held: Pressed or Down. |
IsAnyButtonUp |
bool |
Read-only. Any of the five buttons was let go at the last update. |
IsAnyButtonActive |
bool |
Read-only. Any of the five buttons is pressed, held or just let go. |
MouseButtonInfo members
MouseButtonInfo derives from ButtonInfo, whose properties are the
state and the checks. It adds:
| Member | Description |
|---|---|
Type |
Read-only MouseButtonTypes. Which button it is. |
ToString() |
The type and the state, such as Left | Pressed. |
MouseButtonInfo(MouseButtonTypes type) |
A button of the type, Idle. The mouse status makes its own five; one you create is never updated. |
MouseButtonTypes
| Value | Meaning |
|---|---|
Left |
The left button. |
Middle |
The middle button, usually a press on the wheel. |
Right |
The right button. |
XButton1 |
The first side button, back on most mice. |
XButton2 |
The second side button, forward on most mice. |
The pointer
Position is where the pointer is, in pixels from the top-left corner of the window’s client area, as
MonoGame gives it. Outside the window it goes on counting, so it can be negative or larger than the window.
PositionDelta is how far it moved since the update before: the first update only takes the position,
and its delta is zero, so the game doesn’t see a jump from (0, 0) to wherever the pointer was.
// dragging the map with the right button
if (mouse.RightButton.Down) _cameraOffset -= mouse.PositionDelta.ToVector2();
The delta counts every move of the pointer, the game’s own too. After Mouse.SetPosition, the next delta includes the
jump to the new position, so mouse look that puts the pointer back in the middle of the window each frame measures
from the middle itself:
var center = new Point(Window.ClientBounds.Width / 2, Window.ClientBounds.Height / 2);
var look = mouse.Position - center; // not PositionDelta, which includes the jump back
_camera.Turn(look.X * Sensitivity, look.Y * Sensitivity);
Mouse.SetPosition(center.X, center.Y);
MouseStatus pointer properties
| Property | Type | Description |
|---|---|---|
Position |
Point |
Read-only. Where the pointer is, in pixels from the top-left corner of the window’s client area. |
PositionDelta |
Point |
Read-only. How far the pointer moved since the update before, in pixels. Zero at the first update. |
The wheels
MonoGame keeps a running total for each wheel, and the status keeps it as well as how far it changed:
WheelDelta is how far the wheel turned since the update before, positive away from you, 120 a notch
on most mice. Touchpads and free-spinning wheels send smaller steps, so divide by 120 rather than counting events:
// zoom by 10% a notch, smoothly on a touchpad
if (mouse.WheelDelta != 0) _zoom *= MathF.Pow(1.1f, mouse.WheelDelta / 120f);
The horizontal wheel, a tilting wheel or a two-finger swipe on a touchpad, works the same way, positive to the right. The first update only takes the totals, so scrolling done before it doesn’t arrive as one big delta.
MouseStatus wheel properties
| Property | Type | Description |
|---|---|---|
WheelDelta |
int |
Read-only. How far the wheel turned since the update before: positive away from you, 120 a notch on most mice. |
WheelCumulativeValue |
int |
Read-only. The wheel’s running total, MonoGame’s ScrollWheelValue: 0 when the game starts. |
HorizontalWheelDelta |
int |
Read-only. How far the horizontal wheel turned since the update before, positive to the right. |
HorizontalWheelCumulativeValue |
int |
Read-only. The horizontal wheel’s running total, MonoGame’s HorizontalScrollWheelValue. |
Events
The manager raises four events from inside its update methods, on the game thread:
MouseManager.OnButtonsStatesChanged += states =>
{
if (states.LeftButtonState == ButtonStates.Pressed) _clicks++;
};
MouseManager.OnPositionChanged += position => _cursor.Position = position.ToVector2();
MouseManager.OnWheelDeltaChanged += delta => _list.Scroll(delta / 120f);
OnButtonsStatesChangedis raised once per update when any button changed state, after all five have moved, with a snapshot of the five. Every step counts, not only presses: a click held for a few frames raises it atPressed,Down,UpandIdle. Check the state in the handler, as above.OnPositionChangedis raised when the pointer is somewhere new, also at the first update, unless the pointer starts at (0, 0).Updateupdates the pointer first, so in this handler the buttons still hold their states of the update before.OnWheelDeltaChangedandOnHorizontalWheelDeltaChangedare raised at every update the wheel turns, not only when the amount changes, despite their names.
The events are static: a handler stays subscribed, and keeps its object alive, until it is removed.
MouseManager events
| Event | Handler | Raised when |
|---|---|---|
OnButtonsStatesChanged |
(MouseButtonsStates mouseButtonsStates) |
Static. A button changed state at an update, at any step. Once per update, after all five buttons moved. |
OnPositionChanged |
(Point newPosition) |
Static. The pointer is somewhere new at an update. Before the wheels and the buttons update. |
OnWheelDeltaChanged |
(int delta) |
Static. The wheel turned since the update before. |
OnHorizontalWheelDeltaChanged |
(int delta) |
Static. The horizontal wheel turned since the update before. |
The handler types are delegates nested in the manager: ButtonsStatesChanged,
PositionChanged, WheelDeltaChanged and
HorizontalWheelDeltaChanged.
Snapshots of the buttons
A MouseButtonsStates holds the states of the five buttons at one moment, and never changes after. The buttons
event hands one to its handlers; GetButtonsStates makes one now. Two snapshots compare with ==,
state by state, which tells whether the buttons are as they were:
// the button hints on screen, redrawn only when a button changed
var now = MouseManager.GetButtonsStates();
if (now != _lastButtons) RedrawButtonHints(now);
_lastButtons = now;
A snapshot is a class, so each one allocates. The buttons event makes one at each update it is raised, when it has handlers.
MouseButtonsStates members
| Member | Description |
|---|---|
LeftButtonState |
Read-only ButtonStates. The state of the left button when the snapshot was taken. |
MiddleButtonState |
Read-only ButtonStates. The state of the middle button. |
RightButtonState |
Read-only ButtonStates. The state of the right button. |
XButton1State |
Read-only ButtonStates. The state of the first side button. |
XButton2State |
Read-only ButtonStates. The state of the second side button. |
MouseButtonsStates() |
A snapshot of the five buttons as they are now, the same as GetButtonsStates. |
operator ==(MouseButtonsStates first, MouseButtonsStates second) |
True when the five states are equal, or both snapshots are null. != is its opposite, and Equals compares the same way. |
ToString() |
The five states, such as L: Pressed M: Idle R: Idle X1: Idle X2: Idle. |
Updating part of the mouse
Update reads the mouse once and updates the three parts in order: the pointer, the wheels, then the
buttons. Each part also has an update method of its own, for a game that needs only one of them, or updates them at
different moments. Each reads the mouse again, and each part’s delta counts from that part’s own update before.
A state of your own
Each of the four update methods also takes a MouseState, and does the same with it instead of reading the mouse.
They are for a mouse that never reaches MonoGame’s Mouse, such as an editor viewport’s in a window of its own, see
feeding your own state, and for replays and tests. The status then holds
what the state holds, so give the position in the view’s pixels from its top-left corner, and the wheels as running
totals:
// in the host's wheel event: keep a running total, as MonoGame does
_wheel += e.Delta;
// at the top of the view's update
MouseManager.Update(new MouseState(_pointer.X, _pointer.Y, _wheel,
_left, _middle, _right, _xButton1, _xButton2));
_pointer, _wheel and the five ButtonState fields stand for what your host keeps from its mouse events. This
MouseState constructor leaves the horizontal wheel at 0; the one with a ninth value takes its total last. The
deltas, the events and the first update work as with polling: WheelDelta is how far the total
changed since the update before.
A partial update takes only its part of the state: UpdatePositionData(state) leaves the wheels and the
buttons as they were. Call the polled or the fed updates for the mouse, not both in a frame: they move the same
status.
MouseManager methods
| Method | Description |
|---|---|
Update() |
Static. Reads the mouse once and updates the pointer, the wheels and the buttons, in that order, raising their events. Call it once per frame. |
Update(MouseState mouseState) |
Static. The same, over a state the game built instead of the mouse. Once per frame too, in place of Update(). |
UpdatePositionData() |
Static. Reads the mouse and updates the position and its delta, nothing else. Raises OnPositionChanged when the pointer moved. |
UpdatePositionData(MouseState mouseState) |
Static. The same, with the position of the state passed. |
UpdateWheelsData() |
Static. Reads the mouse and updates the two wheels, nothing else. Raises the event of each wheel that turned. |
UpdateWheelsData(MouseState mouseState) |
Static. The same, with the wheel totals of the state passed. |
UpdateButtonsStates() |
Static. Reads the mouse and moves the five buttons one step, nothing else. Raises OnButtonsStatesChanged when any changed. |
UpdateButtonsStates(MouseState mouseState) |
Static. The same, with the buttons of the state passed. |
GetButtonsStates() |
Static. Returns a new MouseButtonsStates: the states of the five buttons as they are now. |