How do I get input from the mouse?

We will assume that you know how to import Minueto into your project and how to create a MinuetoWindow. If this is not the case, please read How to build your first game Window?.

Getting input from the mouse is very similar to getting input from the keyboard. If you have not read the howto on getting input from the keyboard, please read How do I get input from the keyboard? first.

The get input from the mouse, you must first build a class that implements the MinuetoMouseHandler interface. In other words, your handler must contain the following functions: handleMousePress, handleMouseRelease and handleMouseMove. Those function will be invoke each time a mouse event is processed.

The following example creates a class to handle input for our Demo. When the user presses/releases a mouse button, the application prints the click coordinates.

class DemoMouseHandler implements MinuetoMouseHandler {

    public void handleMousePress(int x, int y, int button) {
        System.out.println("Mouse click on button " + button + " detected at " + x + "," + y);
    }

    public void handleMouseRelease(int x, int y, int button) {
        System.out.println("Mouse release on button " + button + " detected at " + x + "," + y);
    }

    public void handleMouseMove(int x, int y) {
        // Not doing anything on this event.
    }
}

The second step is to build an event queue to receive the mouse input. You'll notice that the mouse event queue looks very similar to the keyboard event queue. In fact, Minueto uses the same event queue for all its messages. Unless you want to process keyboard and mouse events at different times, you can register both handlers on the same event queue.

MinuetoWindow window;
MinuetoEventQueue queue;

window = new MinuetoFrame(640, 480, true);
queue = new MinuetoEventQueue();
window.registerMouseHandler(new DemoMouseHandler(), queue);

If you've worked with MinuetoKeyboardHandler before, you'll know that we need to tell the event queue to handle events in the queue. This is done by calling the handle method. The rendering loop is an ideal candidate since we usually want to process mouse input before rendering each frame.

This code is identical to the code found in the Keyboard Howto. If your application is already handling keyboard input, you don't need to add this code again.

while(true) { 
    while (queue.hasNext()) {
        queue.handle();
    }

    window.render(); 
    Thread.yield();
}

As you might have noticed, capturing mouse input is very simple if you already capture keyboard input. The following code shows how the keyboard howto example can be modified to capture mouse input.

import org.minueto.*; 
import org.minueto.handlers.*; 
import org.minueto.image.*; 
import org.minueto.window.*; 

public class Demo implements MinuetoKeyboardHandler, MinuetoMouseHandler {
    
    public static void main(String[] args) {
        
        MinuetoWindow window; 
        MinuetoEventQueue queue;
        window = new MinuetoWindow(640, 480, true); 
        queue = new MinuetoEventQueue();
        window.registerKeyboardHandler(this, queue);
        window.registerMouseHandler(this, queue);
        window.setVisible(true);

        while(true) { 
            while(queue.hasNext()) {
                queue.handle();
            }

            window.render();
            Thread.yield();
        }
    }

    public void handleKeyPress(int value) {

        switch(value) {
            case MinuetoKeyboard.KEY_Q:
                System.exit(0);
                break;
                default:
                //Ignore all other keys
        }
    }

    public void handleKeyRelease(int value) {
        //Do nothing on key release
    }

    public void handleKeyType(char key) {
        //Do nothing on key type
    }

    public void handleMousePress(int x, int y, int button) {
        System.out.println("Mouse click on button " + button + " at " + x + "," + y);
    }

    public void handleMouseRelease(int x, int y, int button) {
        System.out.println("Mouse release on button " + button + " at " + x + "," + y);
    }
    
    public void handleMouseMove(int x, int y) {
        // Not doing anything on this event.
    }
}

You can learn more about handling input by looking at the handle demos found in the sample directory.