Place to find helpful snippets of code to use in making your own games.

Monday, March 24, 2008

Keyboard Input: Polling System In Java

When I first started game programming and well into about 4 years of it. I always wondered the reason for wanting to use a polling system for Input instead of an interrupt based method. Now I know why and I'll share with you.

I figured it was better to receive an interrupt event than constantly checking the state of every key on the keyboard. But in games its imperative to have our objects updating on a fixed framerate structure and order is primary. Thus if we were to use an interrupt based system it would destabilize when events occur. Sure it is possible to use Java's KeyListener interface for all our keyboard handling needs. But there is a bit more overhead than simply making a single listener that stores the state of each key on the keyboard for us to poll.

The main problem with KeyListener is it tempts us to write all our event handling code in the keyPressed, keyReleased, keyTyped methods which for games is very bad. Since it might take XX amount of milliseconds to execute our event handling code handling multiple events per frame won't do, usually you don't wanna be taking that much time handling your input anyways. The more important reason is that every KeyListener will run in a separate thread while handling the input which means you will have to handle any synchronization issues that might come up while developing.

Those are just a few reasons why you might want to use a keyboard polling system in a Java game.

Basic Start
So lets start with the basics of the idea. For our purposes we'll make it a singleton but if you have an existing design and can integrate it without using a singleton I would recommend it.

You could just store a single integer or short array using different values for the keys state which would allow you more states but since there really aren't any other states boolean arrays will do just fine. So here you can see we have a single variable we can set for each of the 256 keys on the keyboard. This allows us to track multiple keys being pressed simultaneously, needed for fighting games and the like something we would have to do with boolean variables in each KeyListener implementor if we weren't using a single InputManager.

This allows the key state to remain fairly consistent during a single frame in the game update cycle and reset at the end of the cycle. So as long as our update is not taking forever and runs quickly the response time is pretty quick.

What more could you want?
So thats nice but what else, well we sometimes want to just check to see if the user pressed something. We don't necessarily care what it was just if they pressed something. So we add another couple bools for that.

So this allows us to make those nifty little pause screens or making sure we don't eat up the users CPU without making sure their using the game first. Usually you have something like "Press any key to continue" or whatever.

Getting Input
Now we need to actually make our InputManager instantiable by adding the KeyListener interface method implementations.

Here we use the key code as the index for setting each of the key state.

To use the InputManager you will need to add it as a KeyListener, MouseListener, MouseMotionListener to either your JPanels or JFrames that you are using. Also, at the end of every update frame you will want to call the InputManager.update method to reset the key_up_states.

Here is the full InputManager class I use in my Java 2D engine.

InputManager.java - it's back again, let me know if there are issues with this class.

InputManagerTest.java - This is a basic test class which shows how to utilize the InputManager to handle basic keyboard events in a polling loop as well as showing how many events you could receive based on your polling rate.

@Running_Wild: Wow that was a long time ago, hehehe. Yeah I eventually broke down and used your idea since the only way to get direct keyboard access (ie look at the keyboard memory segment) in java is to use custom native code. I try to avoid any native code as it falls under the same cross-platform issues. But this is really the only way to achieve the type of input system you want for a game. I only wish java would have included an input device api, not just for keyboard and mouse. Maybe in the future there will be one. In my opinion it would have been beneficial to do so as new input devices become an increasing part of the future.

One final note, the performance of this class in java is adequate on a mediocre machine (single core 2.8ghz winxp), but could be improved in some ways, like handling straight up awt events, cutting out the swing event layers.

So I added a test class to the post above so that you can see how I've been using this method for polling keyboard input.

https://sites.google.com/site/morbidmorvick/InputManagerTest.java

It's a pretty simple test case to make sure that the InputManager.isAnyKeyDown() and isAnyKeyUp() are working correctly, and from my tests they appear to be working correctly. I also included in there a usage of isKeyUp and isKeyDown and a really simple performance test for how many times the loop detects a key is down.

This isn't entirely related, but it'd be really cool if you could do a post on why exactly a singleton is bad, and what the alternatives are. As a self tought programmer (hobby) I had never come accross the term but have been using them inadvertently, but cannot see why they are so bad, and what alternatives there are?