Xbox Controller Input with Java

There are several external Java libraries that can handle input from game-pads like the Microsoft Xbox controller. However, very few deal exclusively with the Xbox controller and many do not offer complete support. Thus, choosing the correct library is often a matter of the lesser of two evils.We found this library from www.aplu.ch that offers full support for all the buttons on the controller except the home button and is relatively simple to install. The full instructions and the download can be found here:

(installation and download on the right)

NB: as an alternative to placing the .dll files in a folder that is in your system file path (a process that can be difficult or tedious for those less used to more complicated computer work), the files can be directly referenced as the native code for the java libraries. This is done as follows:

1. Open the build path of your java project and open the drop-down menus of the two external jars you added for the Xbox Controller input, then select Native Library Location.

2. Navigate to the folder where both of the .dll files are located and select them.

The method these libraries use to collect controller input is very similar to the way the default Java libraries create Key Listeners. Here is a picture of our code (borrowed heavily from the tutorials posted on the afore mentioned site):

We start here by creating a instance of the XboxController object called xc and then in the constructor we initialize it  in a way in which we can detect whether the system the program is being executed on is 64 bit or 32 bit (using a ternary operator). We then check if the controller is actually connected to the computer and do not let the program start if it is not. Finally, we add a listener to the controller and pass it a new private nested class that extends the object XboxControllerAdapter. In this nested class, we define all the methods that will run when the various buttons on the controller are pressed. These are all specifically named and the full list of the names can be found in the javadoc section of the aplu website under XboxControllerAdapter:

Also, we set the dead zones of the two controller sticks so that they would not detect input until pressed to a certain degree. This is optional but helpful. Furthermore, one should not that frequent errors can occur with this library if it is used in tandem with too many swing components, such as JFrames and JWindows. It is therefore wise to keep ones listener in a class that can be run in a separate thread that is executed over and over. This allows the listener to be restarted if it crashes. Use a scheduled thread pool executor to manage the execution of your listener thread for Xbox input. 

NB: a more detailed example of how to implement this library can be found at the first link listed above at the bottom of the page.  

1 comment:

  1. Amazing Article, thank you!. I am very glad to read your informative & practical blog. Kindly keep updating your blog. Java Developer is a wonderful career for
    IT students.To start Dream Career to become a Java developer learn from Java Training in Chennai.
    or learn thru Java Online Training from India .