OpenTK.Input - jeske/opentk GitHub Wiki

Punctuation lose when imported from wayback machine, apologies for mistakes.

OpenTK provides two distinct methods for mouse input.

  1. OpenTK.Input.Mouse, which provides low-level access to mouse motions. It is independent of windows/displays and provides raw (i.e. non-accelerated) values, if possible. Use this for games that require mouse motions not confined to the display, for instance first-person shooters and 3d games with mouse-controlled cameras.

OpenTK.Input requires OpenTK 1.1 or higher. Its methods are thread-safe and may be used from any thread.

  1. GameWindow.Mouse* events, which are equivalent to WinForms/WPF/GTK# mouse events. The values are accelerated by the OS and are reported in window coordinates. Use this for menu/UI navigation and games that require absolute coordinates, for instance and 2d games. 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.

OpenTK.Input.Mouse

You can move the mouse cursor using OpenTK.Input.Mouse.SetPositionx, 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.GetStateint to retrieve the state of the specified mouse.

To check whether a button is pressed:

using OpenTK.Input;
 
var mouse = Mouse.GetState();
if(mouseMouseButton.Left) {
    // Left mouse button is 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. GameWindow Mouse Input 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.