# [java] using an xbox360 controller? or other USB Controller?

## Recommended Posts

Hey everyone, I've never looked into this, and haven't really found anything on it. I'm looking to get input from my Xbox360 controller (or any USB controller for that matter) and use that to control my games in Java. What all is required, or any tutorials for it online? Thanks!!!

##### Share on other sites
Check out SDL, it allows you to access controllers, I'm pretty sure you can use it with Java. www.libsdl.org Check out the documentation, it's pretty easy, just a few function calls and you have the controller open for use, then you can setup a system to read the data coming from the controller - button presses, joystick movement, etc.

##### Share on other sites
harveypekar    219
A team member of mine is working on exactly the same thing. He's using the jinput library of java.net. He's managed to get the thumbpad and buttons working, and is working on the analog sticks. If you're interested, he would be happy to share his code...

##### Share on other sites
I would be interested! Is it specifically for the Xbox360 or is it from any USB controller?

##### Share on other sites
harveypekar    219
jinput location

jinput tries to abstract evey input device: gamepads, joysticks and whatnots (maybe not the fragmaster :)). You would be programming to gamepads, and try to make the x360 with that in mind.

the following code is something he uses to test his connection to the gamepad. It is actually not specific to the X360 controller. It looks like he'll be finished by Sunday, but I'll just post this code to help you on your way. If you want, I can keep you updated on his progress.

[source lang=java]package net.sourceforge.binge.Xbox360Controller;import net.java.games.input.Component;import net.java.games.input.Controller;import net.java.games.input.ControllerEnvironment;import net.java.games.input.Component.Identifier;public class xbox360ControllerTest {	public xbox360ControllerTest() {		// TODO Auto-generated constructor stub	}		public static void main(String args[]){		ControllerEnvironment ce =			ControllerEnvironment.getDefaultEnvironment();		// retrieve the available controllers		Controller[] controllers = ce.getControllers();				//fetch gamepad controller		Controller gamePadContr = null;		for(Controller c : controllers){			if(c.getType() == Controller.Type.GAMEPAD) {				gamePadContr = c;				break;			}		}				//none found		if(gamePadContr == null){			throw new NullPointerException("No gamepad found");		}				Component buttonComponent = gamePadContr.getComponent(Identifier.Axis.POV);				float prevData = 0;		while(gamePadContr.poll()){			float data = buttonComponent.getPollData();			if(data != prevData)			System.out.println(data);		}	}}

good luck!

[Edited by - harveypekar on March 5, 2007 6:04:14 AM]

##### Share on other sites
harveypekar, hey thanks for the quick reply, I would like to be kept updated... one quick 'n00b' question though, how exactly would I set this up, the jinput site has seemed to confuse me and I don't really understand all I'm looking for. I downloaded the source and the docs, but I don't know where to put them.. like how I'm supposed to install a new package, or get JCreator to use it. I've been taking a Java class in Highschool, and now an AP Computer Science class, but we haven't gotten to where we need to add our own "packages", though I'm sure its a stupid question and can ask my teacher tomorrow, I would perfer to get working on this tonight. Might be able to use my 360 controller as a "final" project for school, and its been something I've wanetd to do since I first got my 360

Thanks!

##### Share on other sites
harveypekar    219
The most easy way would be to get the jar file instead of building from source. Check the menu bar, and click on "document & files". "combined" is the one you're looking for if you want to stay cross-platform. It should have 2 jars, which you will have to add to your build path. All the rest (so's, dll's) should go to the application root.

##### Share on other sites
alright cool thanks! I finally got it compiled and ran the program.. and pushed buttons on my controller and nothing happened, and I'm guessing that is what is supposed to happen so far?

Just keep me updated :-) thanks

##### Share on other sites
Spetzan    122
Hi, i'm Harvey's team member who's working on the xbox360 controller in java :)
We use it in our java game framework (which is currently still in development, you can find it at http://sourceforge.net/projects/binge, but the download is an old version)
If you are using the xbox360 controller, be sure Windows recognizes you're controller.
I'll give you a little tutorial on how to setup Jinput (or atleast how i did it)

Installation
-------------
To setup Jinput, you need 2 files, the JInput zip, and JUtils zip.

https://games-binaries.dev.java.net/build/jutils/jutils-1.1.0-b04-bin.zip

And the latest Jinput zip from the following url:
http://www.newdawnsoftware.com/resources/jinput/jinput_combined_dist_20061029.zip

In the jutils zip, you'll find a jutils.jar file, you need to include this library in your application, together with the jinput.jar file which you'll find in the coreAPI\lib folder in the jinput zip.

after you've done this, you need 3 other files which are in the jinput zip. These files are jinput-dx8.dll, jinput-raw.dll and jinput-wintab.dll. You need to put these files in a folder on your harddisk, and then set the PATH environment variable of your system to this directory.

When you've done all this, you should be ready to start programming with it.

[Edited by - Spetzan on March 7, 2007 3:41:18 AM]

##### Share on other sites
Spetzan    122
Now i'll explain some code i've written to get the buttons of the gamepad to work (thumbpad and analogue sticks and triggers are in the works :) )

first you need to get your environment, where jinput will detect every device you can use with it
************************************
ControllerEnvironment ce =
ControllerEnvironment.getDefaultEnvironment();
************************************

Then you can retrieve the available controllers from the environment (such as gamepad, mouse)
************************************
Controller[] controllers = ce.getControllers();
************************************

You can make a little test to show every controller he finds with the following code
************************************
for(Controller c : controllers){
System.out.println("Name: " + c.getName() + " type: " + c.getType());
}
************************************

To get the gamepad out of the controllers array, use the following code
The only thing it does is traverse through the controllers array, and see if any of them is a gamepad. And if he has found one, he will quit the loop
********************************
for(Controller c : controllers){
break;
}
}
********************************

You can test the green button(A button) of the xbox360 controller with the following code. First i have to get the Button0 component from the gamepad controller (every Controller is composed of some components, like a button or an analogue stick, from which you can get their data). You can get a component from a Controller with the getComponent() method, and give the constant value which corresponds to the component you want.
The variable prevData is used so only data will be printed when you push the button. The value you will get to see will be 1.0, because when a button is pushed the getPollData() method will return 1.0 (this is for buttons, for analogue sticks the values will vary between -1 and 1, and 0 if they are not pressed)
In the while loop I call the poll() method every loop, this is to update the data(states) of the controllers and their components, you have to do this every time, or you won't get the right data.
And to get the current data of the component (so if it's pressed or not), you have to call the getPollData of the Component class.
********************************
float prevData = 0;
float data = buttonComponent.getPollData();
if(data != prevData) System.out.println(data);
}
********************************

So that's it for now, I'll try to post some more when I can implement the analogue sticks, thumbpad and triggers in our project

##### Share on other sites
Guest Anonymous Poster
yea I followed all your directions and entered the code all correctly and so now it completly compiles... but still I've hit every button on the controller and nothing happens... nothing prints out or anything. You have a whole project file right? not just a single java file?

Thanks though for taking all tihs time to help me! I really want to get this to work!

##### Share on other sites
sorry that was me, for whatever reason I Wasn't logged in

##### Share on other sites
Spetzan    122
yes, we have a whole project, but i only posted the code necessary to get it working, because in our project i have coded it to use our framework.
Are you sure Windows detects your xbox360 controller? you can verify this in start -> Control panel -> Game Controllers, and see if your xbox360 controller is in the list, if so, select the controller and click the "Properties" button. now you can click the buttons and use the sticks, and you'll see that he detects it.

And if Windows detects your xbox360 controller, try to see if the gamePadContr controller you get in your java code is not null after you tried to get it.

[Edited by - Spetzan on March 7, 2007 3:29:38 AM]