[Todo: describe keyboard and joystick input]
OpenTK provides two distinct methods for mouse input.
OpenTK.Input requires OpenTK 1.1 or higher. Its methods are thread-safe and may be used from any thread.
Mouse events require OpenTK 1.0 or higher. They are not thread-safe and may only be used on the thread that created the GameWindow.
You can move the mouse cursor using OpenTK.Input.Mouse.SetPosition(x, y). This method will not generate GameWindow events. This method may generate GLControl events, depending on the operating system.
Use Mouse.GetState() to retrieve the aggregate state of all connected mice.
Use Mouse.GetState(int) to retrieve the state of the specified mouse.
To check whether a button is pressed:
using OpenTK.Input; var mouse = Mouse.GetState(); if (mouse[MouseButton.Left]) { // Left mouse button is pressed }
To check whether a mouse button is not pressed:
using OpenTK.Input; var mouse = Mouse.GetState(); if (!mouse[MouseButton.Left]) { // Left mouse button is not pressed }
To check whether the mouse state has changed:
using OpenTK.Input; MouseState current, previous; void UpdateMouse() { current = Mouse.GetState(); if (current != previous) { // Mouse state has changed int xdelta = current.X - previous.X; int ydelta = current.Y - previous.Y; int zdelta = current.Wheel - previous.Wheel; } previous = current; }
To get the mouse coordinates:
using OpenTK.Input; var mouse = Mouse.GetState(); int x = mouse.X; int y = mouse.Y; int z = mouse.Wheel;
Note that these mouse coordinates do not correspond physically to the monitor and should only be used for relative motion. Code that requires physical coordinates should use GameWindow or GLControl mouse events instead.
Use these methods if you want the exact mouse position on the screen. Some examples include:
this.Mouse.ButtonUp += (object sender, MouseButtonEventArgs buttonEvent) => { Console.WriteLine("Mouse button up: " + buttonEvent.Button + " at: " + buttonEvent.Position); };
or
Vector2 pos = new Vector2(this.Mouse.X, this.Mouse.Y);
Where "this" is a GameWindow.