Controlling Multiple Classes with KeyListeners

Started by
-1 comments, last by epicpunnum 11 years, 5 months ago
Hello there! I wanted to ask if there was any way to create a class controllable via KeyListener without the class being a Component or Window. I remember achieving something along those lines by creating a custom child class of KeyListener, however I forget on how I applied it in my code. My closest approach was using this code below, but even still it didn't work.

public Menu(){
Component c = new Component(){};
c.addKeyListener(new KeyListener(){
public void keyPressed(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}

public void keyTyped(KeyEvent e) {
int theCode = e.getKeyCode();
if (theCode==KeyEvent.VK_DOWN)
mIndex++;
}
});
}

I can't find the original code anywhere in my archive of work, so I was wondering if anyone could point me in the right direction.
-Cheers

This topic is closed to new replies.

Advertisement