views:

226

answers:

1

I'm looking for an efficient keyboard input system for a game in Java. At present I'm using some code I found here:

Keyboard Input Polling System

it seems a little inefficient however (please comment as to your thoughts on the code). The full code is available here:

Pastebin Code

I've only just started looking into a new solution and pulling the code apart, but does anybody have any suggestions for an efficient way of doing this in a main game loop (including an explanation)? Any help would be appreciated.

+2  A: 

Set up a KeyEventListener; when invoked, put the current key into a one-key buffer.

In the computation thread, or in a polling thread, query the current key buffer periodically. Now, instead of that constant peeking at the I/O, you have the event listener doing it (which is already heavily optimized and cleanly implemented to use the underlying hardware) while you keep having your periodic polling.

Charlie Martin