I decided to randomly start a small OpenGL 2D game, and the problem is, my keyboard input isn't working. There are no errors or anything, however, the little square I've created refuses to move. Here's my player class:

public class Player {

 public int x = 0;
 public int y = 0;
 static int walkSpeed = 2;

 private InputHandler input;

 public Player() {


 public void update(){
    input = new InputHandler();

    if (input.up) y--;
    if (input.down) y++;
    if (input.left) x--;
    if (input.right) x++;

 public void render() {
    glTranslatef(x, y, 0);

    glVertex2i(300, 300);// upper left
    glVertex2i(350, 300);// upper right
    glVertex2i(350, 350);// bottom right
    glVertex2i(300, 350);// bottom left

And my input handler:

public class InputHandler implements KeyListener{

private boolean[] keys = new boolean[120];  
public boolean up, down, left, right, exit;

public void update(){
    up = keys[KeyEvent.VK_W];
    down = keys[KeyEvent.VK_S];
    left = keys[KeyEvent.VK_A];
    right = keys[KeyEvent.VK_D];
    exit = keys[KeyEvent.VK_ESCAPE];

public void keyPressed(KeyEvent e) {
    keys[e.getKeyCode()] = true;

public void keyReleased(KeyEvent e) {
    keys[e.getKeyCode()] = false;

public void keyTyped(KeyEvent e) {


I call the keylistener in the main class like this:

public class Main extends Canvas{
long lastFrame;
long fps;
long lastFps;
private InputHandler key;

public Main(){
    try {
        Display.setDisplayMode(new DisplayMode(800, 600));
        Display.setTitle("OpenGL Platformer");
    }catch (LWJGLException e){


    lastFps = getTime();
    key = new InputHandler();

Specifically this code adds the listener:


What should I do? Any help would be greatly appreciated

  • 1
    \$\begingroup\$ This looks like Java, and OpenGL doesn't deal with key input (or any input at all), so I've adjusted the title and tags accordingly. \$\endgroup\$ – user1430 Dec 24 '12 at 15:32
  • 2
    \$\begingroup\$ Beyond that, the code-to-question ratio here is a bit unfavorable, which is generally a sign the question is far too localized. Your best bet is to set some breakpoints or otherwise explore in your debugger and see where execution goes where you don't expect, or examine the documentation for KeyListener and see if you're doing anything wrong. A bunch of code plus "it doesn't work" doesn't give us much to go on, unfortunately. \$\endgroup\$ – user1430 Dec 24 '12 at 15:34
  • \$\begingroup\$ In LWJGL you may use the org.lwjgl.input.Keyboard Class to fetch Keyboard inputs. \$\endgroup\$ – Aron_dc Dec 30 '12 at 23:09
  • \$\begingroup\$ You know this question was closed right? -_- @Aron_dc \$\endgroup\$ – opiop65 Dec 31 '12 at 2:56
  • \$\begingroup\$ Yeah but I why shouldn't I give advice just because it's closed? Maybe someone else will stumble across this and is still happy to find that piece of information I left behind ;). \$\endgroup\$ – Aron_dc Jan 13 '13 at 22:47

You have a second inputHandler inside your player class. This handler has not been added as a key listener. Further you create it again every update. Just create it in the player class constructor and then add it to your key listeners.

More importantly, learn to use the debugger and set break points. It'll help you a lot for situations like this. If you already know how to use the debugger. Learn to use it better and start using it. It's invaluable for your development career.


Not the answer you're looking for? Browse other questions tagged or ask your own question.