3

I seen many conflicting recommendations on the internet including here on how to handle input with awt and swing and several people have worked on my code and its a mess.

options

  • implement KeyListener or extend KeyAdapter
  • ^to the application's main class, use an anonymous class, use a private class or use an external input managing class.
  • to send the event object to each object that needs to know input, to send an array of keys pressed, or to make every class a listener and add it to the main class.

So I could have

public class Board extends JPanel implements KeyListener{
    public Board(){addKeyListener(this);}}

or

public Board(){addKeyListener( new KeyListener(){...});}

or

public class Board extends JPanel {
    private class PrivateListener implements KeyListener{...} 
    public Board(){addKeyListener(new PrivateListener());

or

public class PublicListener implements KeyAdapter{...}
public class Board extends JPanel {
    public Board(){addKeyListener(new PublicListener());

or

addKeyListener(this);
addKeyListener(obj1);
addKeyListener(obj2);

and implements KeyListener can be replaced with extends KeyAdapter but I won't do that because java only allows for one parent class.

then there is which I don't know how this got into my code

private boolean [] keys = new boolean[256];
public void keyPressed(KeyEvent e) {keys[e.getKeyCode()] = true;}
public void keyReleased(KeyEvent e) {keys[e.getKeyCode()] = false;}
public boolean isKeyDown(int k) {return keys[k];}

or

public void keyPressed(KeyEvent e) {
    obj1.keyPressed(e);
    obj2.keyPressed(e);
}

Truly, what is the best implementation of awt keyboard input?

Old Badman Grey
  • 668
  • 10
  • 19
  • *"implement KeyListener.."* You did not see anything about [Key Bindings](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) in those searches? – Andrew Thompson Oct 30 '13 at 03:43
  • BTW - is the code using AWT **components**? If so.. Why AWT rather than Swing? See this answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. If you need to support older AWT based APIs, see [Mixing Heavyweight and Lightweight Components](http://www.oracle.com/technetwork/articles/java/mixing-components-433992.html). – Andrew Thompson Oct 30 '13 at 03:45
  • @AndrewThompson I was working on a 2d awt based game. In doing such I found many ways to do controls. The question remains the best way to do input with awt (also life sucks when it comes to hobbies) – Old Badman Grey Mar 31 '14 at 07:49

1 Answers1

0

This is a solid "it depends" situation, and this answer is open to plenty of academic discussion.

My Oppinion:

This is definately just an oppinion, but I would recomend talking to your coders and agreeing on two things. Make your decision based on the answers, and put it in the README at the base of your code:

  1. Anonymous classes, or not?
  2. Mega classes, or not?

Assuming prefering small, named classes I would go for the following.

public class PublicListener implements KeyAdapter{...}
public class Board extends JPanel {
    public Board(){addKeyListener(new PublicListener());

Using KeyAdapter you will only have to override the functions you care about, and other programmers can see quite easily that this is where the input is handled as it is in its own class.

TLDR

Firstly, wanting to work with low level classes is a valid desire. In production code I would not recomend it, but AWT is still the basis of Swing, and wanting to learn is not to be discouraged! If nobody learns the low level stuff, then one day the open source community will have a big problem being built on top of code, which nobody has learned.

While the comments on Swing and Key Bindings are valid, in the documentation about Swing Key Bindings, there is the quote:

An alternative to key bindings is using key listeners. Key listeners have their place as a low-level interface to keyboard input, but for responding to individual keys key bindings are more appropriate and tend to result in more easily maintained code. So if you are in it to learn some low level stuff, go for it, but take note of the maintainability warnings!

The rest comes down to two main point. Coding Style and Application Architechture.

Coding Style:

Consistency across a code base is often more important than having the perfect implementation, so if you have a codebase with an existing style, then stick to that style. For example, if your codebase already uses lots of Anonymous Classes in other places for other things, then adding a new KeyListener(){...} is fine. That said, in my personal projects I avoid anonymous classes, because personal preference...

The decision for (what I call) Mega and Mini classes is also important in coding style. The very clear recomendation is to prefer small (mini) classes, with specific roles. This would support implementing a dedicated class which extends KeyAdapter. Mini classes rarely struggle from multiple inheritance problems, allowing you to rely on default implementations (like KeyAdapter), further reducing your code complexity. Future coders can then very easily see what that class is for. Mega classes centralise all of the code into one java file, but then you will be implementing lots of interfaces, and also having to provide default implementations for everything. Compairing KeyListener to KeyAdapter this means implementing three functions instead of overriding one.

Mega classes have their place because multiple inheritance. A "Player" can be a Human, "PositionProvider", a "Drawable" etc. But in all seriousness, this is just not recommended.

Architechture:

Games programming was mentioned, so this is also an important consideration. The architechture of a game can be very different to a dektop GUI application. You need to pick an architechture, and make code which sticks to it. E.g. are you planning to let several components listen to the AWT events and act independantly (entities), or do you plan to have a seperate "InputManager" class, which abstracts input from e.g. keyboard and joypad?

Luke Turner
  • 324
  • 1
  • 4
  • 22