1

I'm coding a game where the player moves around by using the WASD keys. I know that there are two ways to register keyboard input for this situation: by using key listener or key adapter.

However, it hasn't been very clear what the benefits of each are, and there would be no reason for the Java creators to have two classes that do the same thing.

I want to know which would be the better option.

3 Answers 3

2

public abstract class KeyAdapter extends Object implements KeyListener

An abstract adapter class for receiving keyboard events. The methods in this class are empty. This class exists as convenience for creating listener objects.

Actually seems that KeyAdapter is a class that implements KeyListener interface thus we cannot talk about which one is better since the implementation and interface are not equal

What you actually can do is just to extend KeyAdapter class and add some additional support for keyboard events depending on what you actually need

Here you can find examples of how to approach this topic

2
  • KeyAdapter (abstract class): One need not implement all the methods - just provide the code for required method(s). In case the application extends this class there will not be a chance to extend any other classes.
  • KeyListener(interface): In case of implementing this interface, one need to implement all the three methods. But, there is a possibility for extending other classes, later.

I want to know which would be the better option.

This depends upon your application design. Some sample scenarios:

public class MyGuiApp extends KeyAdapter implements ActionListener {
    // Override any -or- all of the three KeyAdapter class methods as needed
    // No possibility of extending any other class
}

public class MyGuiApp implements KeyListener, ActionListener {
    // Override all of the three methods of KeyListener
    // Can have empty method bodies for the ones not used
    // Possibility of extending any other class, later
}

public class MyGuiApp {
    // ...
    public void buildGui() {
        // ...
        JButton button = new JButton("Press me");
        button.addKeyListener(new MyKeyListener());
        // ...
    }

    // NOTE: Application will need only one of the implementations, in this sample.
    // KeyAdapeter -or- KeyListener

    private class MyKeyListener extends KeyAdapter {
        // NOTE: Only one method is implemented - in this case
        @Override public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_RIGHT){
                        // do some ui related action...
                }
        }
    }

    private class MyKeyListener implements KeyListener {
        // NOTE: Only all methods are reuired to be implemented - in this case
        @Override public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_RIGHT){
                        // do some ui related action...
                }
        }
        @Override public void keyReleased(KeyEvent e) {
            // No code - as its not used
        }
        @Override public void keyTyped(KeyEvent e) {
            // No code - as its not used
        }
    }
}


Example code links:

0

There is ONE small difference:


KeyListener

The compiler will show error

error: <anonymous class$1> is not abstract and does not override abstract method                 
keyReleased(KeyEvent) in KeyListener

And in IDE You will get

Add UnImplemented Methods

KeyAdapter

You will NOT get any error message


Hence, It is convenient to use KeyAdapter, if you want to override some methods only.

1
  • It's been almost three years since I posted this question. Thanks for your reply though
    – Reschivon
    May 26, 2021 at 6:02

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.