Best way of handling input?

Hi all,

Looking for a bit of advice here on the best way of how to handle input in my game engine. I don’t have any code to show you (well, none that works completely) but I do have several ideas - but really they seem to suck/make it difficult for the end developer who is creating the game.

Idea 1.

Have some class that extends KeyListener and has 3 variables that store the keycode for each key pressed, released etc. Then, an update method in my engine class will somewhere call the method getKeyPressedKeyCode() for example on that class, and then have a switch statement to decide what to do (i.e. if left key pressed, decrease x by 5.0 on some sprite).

However, could there be any chance of being multiple key presses in one gameloop, or could input be missed?

Idea 2.

Have another class, possibly something like EventQueue that collects all the events from a key being pressed and released etc. When this happens, in another class (Keyboard) I would have a collection of all the keycodes/keyevents and depending on which was pressed, from the EventQueue it would set them to true/false.

Then, in the update method in the game engine, there would be again checks to decide what to do based on what keys are pressed.

Does anyone have any better ideas?

My main problem is, as this is a game engine and not just a game, I’m trying to make it kind of simple of the developer. Therefore I cannot set a variable in say the SpriteManager to say (moveLeft == true) because one developer may want the left arrow key to be something else, or not move left at all etc. Therefore I’m trying to be as generic as possible (which is difficult!)

So any suggestions, ideas would be gratefully received and should get me moving on with the rest of my engine!

Thanks!

Just a FYI: I’m using Java2D for displaying graphics on the screen, not something like LWJGL. I wish I had of done now, particularly as there seems to be classes there to handle input etc, but with 6 weeks left on this, I don’t think there is time to change now!

You could check out this tutorial I wrote. It handles input from Java2D.

Here’s a system that I’m pretty happy with: http://code.google.com/p/ferox-gl/source/browse/#svn%2Ftrunk%2Fferox-core%2Fsrc%2Fcom%2Fferox%2Finput%2Flogic

It tracks the entire state of the keyboard and mouse (i.e. which keys are down, which mouse buttons are down, where the mouse is). Every event gets processed in order, creating a new state to match what the event did (i.e. release a key). This creates a list of states that then get processed on request in the main loop. This lets processing happen in the main loop but will not miss events (which can happen if you just use a single state and frame rates are low).

When processing the states, there are triggers that take the previous and current state that can be used to build conditionals, such as “run X when ‘A’ is pressed the first time and the mouse is dragging”. This design has been heavily influenced by the input system in the Ardor3D engine, so I don’t mean to take credit for it.