myusername123
myusername123

Reputation: 49

Java Key Listener doesn't work

today i tried to include a KeyListener into my first java problem. I searched on multiple sites for a solution, but sadly didn't found one.

Maybe someone of you guys, could have a look at it.

    public class AL extends KeyAdapter {
        public AL() {

        }

        public void keyPressed(KeyEvent e) {
            key = e.getKeyCode();

            if(key == KeyEvent.VK_LEFT) {
                System.out.println("Left Button pressed");
            }
        }
    }

Full code: http://pastebin.com/FfdHymFp

Upvotes: 1

Views: 75

Answers (1)

Nicolas Filotto
Nicolas Filotto

Reputation: 44965

You need to add your KeyListener to a Component using the method addKeyListener(KeyListener) otherwise it cannot work.

Please have a look to this doc

Upvotes: 1

Related Questions