LWJGL Basics 2 (Input)

From LWJGL
Jump to: navigation, search

Contents

Introduction

LWJGL handles its own input with its Keyboard and Mouse classes. In order to get updates the Keyboard and Mouse classes must be polled. This is done automatically when you call the Display.update() method therefore the poll() method in those classes does not need to be called manually.


The Mouse

The position of the Mouse on the Display can be obtained by calling the following methods:

int x = Mouse.getX(); // will return the X coordinate on the Display.
int y = Mouse.getY(); // will return the Y coordinate on the Display.

The 0,0(x,y) position of the mouse will be in the bottom left corner of the Display (in traditional OpenGL style).

The Mouse.isButtonDown(int button) method will return true if the mouse button is down or false if not. The mouse buttons are numbered 0 to buttonCount.

boolean leftButtonDown = Mouse.isButtonDown(0); // is left mouse button down.
boolean rightButtonDown = Mouse.isButtonDown(1); // is right mouse button down.


The Keyboard

The Keyboard.isKeyDown(int key) method will return true if the relevant key is down or false if not. A full list of Keyboard keys can be found on the Keyboard javadoc here.

boolean keyDown = Keyboard.isKeyDown(Keyboard.KEY_X);  // is x key down.


The Event Buffer

Using the isKeyDown() or isButtonDown() methods can work well, however its possible to miss key or button presses if polling isn't done fast enough.

To get around this problem the event buffer can be used. This buffer holds all the events from the keys and buttons received by the Display. The events in the buffer are accessed using the getEvent*() methods.

The next() method is used to scroll through the event buffer. Each time the next() method is called it will set the next event from the buffer as the event key/button and once all the event keys/buttons have been scrolled through it will return false.


For example the Keyboard buffer can be scrolled through using the following:

while (Keyboard.next()) {
    // get event key here
}


The Keyboard.getEventKey() method will return which key generated the event.

if (Keyboard.getEventKey() == Keyboard.KEY_A) {
    System.out.println("A Key Event");
}


The Keyboard.getEventState() method will return true if the event key was pressed or false if it was released.


Using the above two methods one can detect which key generated the event and whether it was pressed or released.

if (Keyboard.getEventKey() == Keyboard.KEY_A) {
    if (Keyboard.getEventKeyState()) {
        System.out.println("A Key Pressed");
    }
    else {
        System.out.println("A Key Released");
    }
}


The example below will run through all the events in the buffer and test the key events therein for the A key.


while (Keyboard.next()) {
    if (Keyboard.getEventKeyState()) {
        if (Keyboard.getEventKey() == Keyboard.KEY_A) {
	    System.out.println("A Key Pressed");
        }
    }
    else {
        if (Keyboard.getEventKey() == Keyboard.KEY_A) {
	    System.out.println("A Key Released");
        }
    }
}


The full API for both the Keyboard and Mouse classes can be found on their Javadoc pages.


Example

A full working example is below.


import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

public class InputExample {

    public void start() {
        try {
	    Display.setDisplayMode(new DisplayMode(800, 600));
	    Display.create();
	} catch (LWJGLException e) {
	    e.printStackTrace();
	    System.exit(0);
	}

	// init OpenGL here

        while (!Display.isCloseRequested()) {

	    // render OpenGL here
			
	    pollInput();
	    Display.update();
	}

	Display.destroy();
    }

    public void pollInput() {
		
        if (Mouse.isButtonDown(0)) {
	    int x = Mouse.getX();
	    int y = Mouse.getY();
			
	    System.out.println("MOUSE DOWN @ X: " + x + " Y: " + y);
	}
		
	if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
	    System.out.println("SPACE KEY IS DOWN");
	}
		
	while (Keyboard.next()) {
	    if (Keyboard.getEventKeyState()) {
	        if (Keyboard.getEventKey() == Keyboard.KEY_A) {
		    System.out.println("A Key Pressed");
		}
		if (Keyboard.getEventKey() == Keyboard.KEY_S) {
		    System.out.println("S Key Pressed");
		}
		if (Keyboard.getEventKey() == Keyboard.KEY_D) {
		    System.out.println("D Key Pressed");
		}
	    } else {
	        if (Keyboard.getEventKey() == Keyboard.KEY_A) {
		    System.out.println("A Key Released");
	        }
	    	if (Keyboard.getEventKey() == Keyboard.KEY_S) {
		    System.out.println("S Key Released");
		}
		if (Keyboard.getEventKey() == Keyboard.KEY_D) {
		    System.out.println("D Key Released");
		}
	    }
	}
    }

    public static void main(String[] argv) {
        InputExample inputExample = new InputExample();
	inputExample.start();
    }
}


Credit

Tutorial Credit - Ninja Cave

Personal tools
Namespaces

Variants
Actions
Navigation
Tools