The Source for Java Technology Collaboration


Jeff on Input

How do I detect and read joysticks in Java?

There are two steps to game-style input. The first is "discovery". This is how you find out what input devices are attached to your computer. This can include keyboards and mice as well as joysticks, gamepads, steering wheels or any other form of input device. Once you have discovered the input device, you read its state by polling. This is a tecnique where once per frame you read the current state of the controller and respond to it.

Controller discovery and polling is supported on Macintosh, Windows and Linux through the open source JInput API. (http://jinput.dev.java.net). JInput takes the platform specific mechanisms (DirectX? in Windows, HID Manager in OSX and Linux system calls in Linux) and makes then all look the same to the Java programmer, so the same Java input code works unchanged across all 3 platforms. It also supports plug-ins for controllers that are not supported through the standard platform mechanisms.

Key repeat failure on Linux

If your game depends on the user to hold down a key, such as the typical WASD movement keys in a First Person Shooter, you are likely to run into an odd effect on Linux. It will work briefly and then suddenly the key will stop responding until you let go and press it again. This is X windows trying to be smart. It sees that key held down and after a short period goes "oh it must be stuck" and turns it off.

The solution is the command xset -r which turns off all the X key repeat logic and just passes the state directly on to your program like you really want.

-- Main.jeffpk - 22 Jan 2006

Topic JeffOnInput . { Edit | Ref-By | Printable | Diffs r2 < r1 | More }
 XML java.net RSS

Revision r2 - 12 Feb 2006 - 21:10:59 - Main.jeffpk
Parents: WebHome > FAQ > JeffFAQ