Java KeyListener in separate class

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2011-02-27T04:58:09Z Indexed on 2011/03/07 16:10 UTC
Read the original article Hit count: 263

Filed under:
|
|

So I have my main class here, where basically creates a new jframe and adds a world object to it. The world object is basically where all drawing and keylistening would take place...

public class Blobs extends JFrame{

    public Blobs() {
        super("Blobs :) - By Chris Tanaka");
        setVisible(true);
        setResizable(false);
        setSize(1000, 1000);
        setIgnoreRepaint(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(new World());
    }

    public static void main(String[] args) {
        new Blobs();
    }
}

How exactly would you get key input from the world class? (So far I have my world class extending a jpanel and implementing a keylistener. In the constructor i addKeyListener(this). I also have these methods since they are auto implemented:

public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_W)
        System.out.println("Hi");
}

public void keyReleased(KeyEvent e) {}
public void keyTyped(KeyEvent e) {}

However this does not seem to work?

© Stack Overflow or respective owner

Related posts about java

Related posts about key