LWJGL Input

By the end of this tutorial you will have learned how you can receive and process LWJGL input.

Everything is static

Similarly to the Display creation, the Lightweight Java Game Library input API is static.

Polling versus event queue

There are two forms of input handling. Either check the state at the current moment in time (polling) or read events from the event queue. The first method is preferred for movement with the arrow keys or the mouse, while the second method is preferable for when a key is briefly pressed to activate a power-up or click on a menu using the left mouse button.

This graph illustrates the difference between polled and event-driven input. While polled input gives out a signal as long as the button is pressed, the event-driven input only gives out a signal once for every time a button is pressed or released.

Polled LWJGL input

The following code demonstrates polled input.

if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
	player.moveToLeft();
}

Event-driven LWJGL input

The following code demonstrates event-driven input.

while(Keyboard.next()) {
	if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
		jump();
	}
	if (Keyboard.isKeyDown(Keyboard.KEY_F)) {
		pickUpItem();
	}
}

Replace Keyboard with Mouse and ‘isKeyDown’ with ‘isButtonDown’ and the same applies to the mouse input. Use ‘Mouse.getX()’ and ‘Mouse.getY()’ to retrieve the coordinates of the mouse in a non-event-driven style. Similarly, you can use ‘Mouse.getDX()’ and ‘Mouse.getDY()’ to retrieve the displacement over the x-axis or the y-axis. Please note that to retrieve the correct y-coordinate with the projection matrix we created last tutorial, you need to call ‘Display.getHeight() – Mouse.getY() – 1′. If you use ‘Mouse.getDY()’ prefix it by ‘-‘. If this step is omitted, the y-value will be the opposite of the correct value.

Conclusion

That concludes this tutorial. Should you have any questions or remarks, please leave them in the comments section below.

15 thoughts on “LWJGL Input

  1. Jokull Reynisson

    Hey, I love your tutorials as they are very helpful!

    I have run into a problem that I can’t seem to find the answer to and I’m hoping that you could shed some light on it at all?

    I have a retina macbook pro and everything works as intended except for Mouse.getDX() and Mouse.getDY()… I have tried doubling it and various different ways of trying to correct for the resolution difference between retina and normal displays. No matter what I do, if I move the mouse fast enough DX and DY begin to return increasingly incorrect values.

    I will continue to try to fix this and implement my own fixes in the meantime

    Reply
  2. Hillel

    First of all, thank you for your tutorials! I’ve managed to implement gamepad support because of you, and it works great! However, in the event driven input snippet, I don’t like the looks of that while loop. Where do I put it in a game? In the standard input system (listeners) they have sort of a thread for themselves, but this has no context. Can you please elaborate?

    Reply
  3. Namphiz

    Your tutorials are very good (both video and text)! Don’t stop making them!

    But it would be good if you explain more why we need to do something special (if you know it).

    For example this sentence:
    “Please note that to retrieve the correct y-coordinate with the projection matrix we created last tutorial, you need to call ‘Display.getHeight() – Mouse.getY() – 1′.”

    Why do I have to use this “special” way?

    Reply
    1. Oskar Veerhoek Post author

      Because by default the mouse coordinates start at the bottom-left corner of the window. Since our coordinate system specifies that (0,0) is the top-left corner of the window, we have to modify the y-coordinate.

      Reply

Leave a Reply to Hillel Cancel reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>