-
Notifications
You must be signed in to change notification settings - Fork 2
Mouse
#The Mouse There are three main operations for the mouse: Get the mouse position, set the mouse position, and get the state of the actual mouse buttons.
#Get position The following getters will return the mouse position in screen space
public int MouseX { get; }
public int MouseY { get; }
public Point MousePosition { get; }
Sometimes you don't want to get the actual position of the mouse, just check if it's been moved left or right (Like a first person shooter). The following getters will have a value of -1.0f to 1.0f, depending on how fast you moved the mouse.
public float MouseDeltaX { get; }
public float MouseDeltaY { get; }
public PointF MouseDelta { get; }
#Set position You can either set the position of the mouse to some screen space pixels, or just center it. When would either of these functions be useful? First person shooters will check the delta motion of the mouse, adjust the camera accordingly and then center the mouse to screen. The functions to set mouse position
public void SetMousePosition(Point newPos)
public void CenterMouse()
The mouse's delta position is 0 if the mouse has not moved. X is negative if the mouse moved left, positive if the mouse moves right. The Y is negative if the mouse moves up, positive if it moves down.
#Check buttons
We have four button states we can check. MouseDown, MouseUp, MousePressed and MouseReleased. Because the low level input system is handled by OpenTK all of these functions take an OpenTK enumeration as argument. The enumeration they take is of type OpenTK.Input.MouseButton. There are three specific enumeration values that interest us (We will only be using these three), they are MouseButton.Left, MouseButton.Right and MouseButton.Middle.
- MouseDown: True if the given mouse button is being held down
- MouseUp: True if the mouse button is resting (is not held or pressed)
- MousePressed: True for one frame, the moment that the button is pressed
- MouseReleased: True for one frame, the moment that the button is released
The signatures of these functions look like this:
public bool MouseDown(OpenTK.Input.MouseButton button) {
public bool MouseUp(OpenTK.Input.MouseButton button) {
public bool MousePressed(OpenTK.Input.MouseButton button) {
public bool MouseReleased(OpenTK.Input.MouseButton button) {
#Using Not important for this project, but when you are working on your game, it's a good idea to add
using OpenTK.Input;
to your using block, to get access to the mouse, keyboard and joystick button enumerations without having to fully qualify them. Add that using block to any file that checks for input!
#Example application All of the above methods are pretty self explanatory, i don't think we need to go trough them in any more detail. Let's try to build a sample application, to demonstrate some of this functionality. What will the application do?
- Show the mouse position on screen
- Show the mouse delta movement on screen
- Toggle centering the mouse to the screen with a left click
Here is my exe file to give you an idea of how it works, check it out if you want to.
Lets get started, draw a string to screen that displays the mouses X and Y positons, the string can be anywhere. Your app should look like this:
Next, somewhere on screen draw a string that contains the delta position on the mouse. This for the most part will be 0, remember you have to move the mouse to draw a value, it will probably flash too fast for you to see. Your app should look something like this:
Finally the toggle. Add a static boolean to your class, by default it will be false, i called mine mouseIsCentered. In the Render method, draw it's value (for now always false). Your app should looks kind of like this:
For now the bool does nothing, let's hook it up. Inside of the Update method, after you update the InputManager, check if the left mouse button IsPressed, if it is toggle the member bool
mouseIsCentered = !mouseIsCentered;
Running the application now, every time you click on the screen the last string we printed should change. MAKE SURE that every time you click on the window this changes before moving on! Lets add some functionality next. After the if statement that sets the mouse centered lets add another if statement.
In Update still, if mouseIsCentered is true call the CenterMouse function of the input manager. You should now be able to toggle a centered mouse. If you set it to true the mouse will be centered to the window at all times.
If you are having trouble with the code, or are just curious, check out my Program.cs file

