Mouse
Last updated
Was this helpful?
Last updated
Was this helpful?
The Mouse class provides functionality for getting input data from the physical mouse. This class is automatically instantiated by and accessible through the .
There are many ways to get data from the mouse. The following section of code is a series of if-statements which could be used to detect input from the mouse.
The mouse scroll wheel is exposed through the InputManager.Mouse.ScrollWheel
property. The ScrollWheel
property returns the number of "clicks" that the scroll wheel has moved since last frame. The following code controls the Z position of the camera based on the ScrollWheel
property.
You can check whether the value is positive or negative to check if it's scrolled in or out:
The following code shows how you might move a 3D Camera (zoom in or out) based on the scroll wheel.
The Mouse class provides information about the cursor's pixel coordinates.
The mouse coordinates are top left justified, so (0,0) represents the top left corner of the screen. Keep in mind that the pixel coordinates are relative to the top-left of the screen, but not bound by your game screen. That means that if the cursor is to the left (outside of) your game screen, then the X value will be negative. Also, the mouse will continue to return values when the cursor is to the right or below the game screen, resulting in values which are potentially larger than the width or height of the screen.
The Mouse reports the world coordinates of the cursor through the functions WorldXAt and WorldYAt.
Certain activities should only occur if the mouse is in the window. To test for this, use the following code: Add the following using statement:
The following code performs the check: