Skip to main content
GameDev.net gamedev.net
🔒 Locked

Controlling Multiple Classes with KeyListeners

Started by epicpunnum Oct 30, 2012 at 12:19 AM 0 replies 1.2k views
Original Post
epicpunnum
epicpunnum
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.