Java KeyListener in separate class
- by Chris
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?