Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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() {
    render();
    update();

 }

 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);

    glBegin(GL_QUADS);
    glVertex2i(300, 300);// upper left
    glVertex2i(350, 300);// upper right
    glVertex2i(350, 350);// bottom right
    glVertex2i(300, 350);// bottom left
    glEnd();
 }
}

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");
        Display.create();
    }catch (LWJGLException e){
        e.printStackTrace();
    }

    getDelta();

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

Specifically this code adds the listener:

        addKeyListener(key);

What should I do? Any help would be greatly appreciated

share|improve this question

closed as too localized by Byte56, Darth Satan, Trevor Powell, Sean Middleditch, bummzack Dec 28 '12 at 9:26

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
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. –  Josh Petrie Dec 24 '12 at 15:32
2  
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. –  Josh Petrie Dec 24 '12 at 15:34
    
In LWJGL you may use the org.lwjgl.input.Keyboard Class to fetch Keyboard inputs. –  Aron_dc Dec 30 '12 at 23:09
    
You know this question was closed right? -_- @Aron_dc –  opiop65 Dec 31 '12 at 2:56
    
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 ;). –  Aron_dc Jan 13 '13 at 22:47

1 Answer 1

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.

share|improve this answer

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