Joystick in Java with JInput v2

Posted: October 26, 2013 in Java
Tags: , , ,

Once I already done this program to test joystick in Java with JInput but it wasn’t enough dynamic. So here’s version two. I thought that every joystick of stick type or gamepad type have the same components, but that’s not how it is. So here is the program that adds controller components dynamically, and also it founds all joysticks that are connected to computer.

Joystick in Java with JInput

JInput is Java game controller API, you can fount it here.

To use JInput with your Java application you need some JInput files/libraries. You can download latest JInput on link above or use the one that are in zip package on the bottom of this page.

How to use JInput?

First you need to move JInput files to certain places and link jar file to your project. I will describe how I did it in NetBeans, probably is almost the same with Eclipse.

  1. First unzip jinput zip package that you downloaded and move “jinput.jar” somewhere for later use, I usually copy jar libraries files into my java installation dir (C:\Program Files \Java\) and then link them to project from there. Or use jinput.jar in jinput folder that I put into zip package on the bottom of this page.
  2. Then you need to move other files into root director of your project. I already done this in the project that you can download on the bottom of this page.JInputJoystickTest root folder
  3. Now go to NetBeans and click on folder named “Libraries” in your project with right mouse button and select “Add JAR/Folder…” then locate “jinput.jar” file. Now should jar file be visible in “Libraries” folder.

Adding jar file to a project in NetBeans

Now that you put all JInput files/libraries where they need to be, you can start using JInput. Note that files (jinput-dx8.dll, jinput-raw.dll, …) that you put into your root folder of a project are used when you run project inside the NetBeans. When you will build your project you will need to copy this files (jinput-dx8.dll, jinput-raw.dll, …) into folder where your game/app jar file will be (into “dist” folder, inside your project folder when you build a project). File “jinput.jar” will be copied automatically.

Download

JInputJoystickTestV2.zip

Here you can download zip package that contains NetBeans project folder, in this folder you can also found jinput files. To download you have to click File>>Download.

Advertisements
Comments
  1. Dani says:

    This is even better than the previous one. Great job!!!
    Thanks!,

    Dani

  2. juho says:

    it doesnt detect my qtronix orion90a on linux

  3. David says:

    Hey Nice work,thanks.
    Is it possible to trigger an action when I press some button on the controller?

  4. yalharthi says:

    Thanks for the Class
    i have something to ask..
    if i want for example make the buttons do some action or events like copy text field if you press button like (Y) in Xbox360 ,,
    would you plz give example how to do it..

  5. Teacher77 says:

    Great work! It helped me a lot to write a program controlled by a ps2-buzzer. But it works only when started from Netbeans. When building a jar no controller is found. Can anybody help me?

  6. cristian vantini says:

    the .jar works fine !
    …i can NOT compiling all source files
    bot by command line and eclipse !?!?!?!

  7. ethan says:

    I have a question. Why is the line of code,

    Controller[] controllers = ControllerEnvironment.getDefaultEnvironment().getControllers();

    ,always returning the same controllers that were plugged in initially, even after some of them have been unplugged? I’m assuming it has to do with the default environment not checking to see if the state/number of the controllers has changed. Is there any way to get a updated list of plugged in controllers? I already know that the poll() method will return false when a controller is unplugged, but i can not seem to get controllers that have been plugged in after the application was started.

  8. Just a little note in order to thank you and tell everything was fine in the following environment:

    – Java 8 update 20
    – Netbeans 8.0.1
    – Thrustmaster T.16000m

  9. Maxl says:

    Hi,
    is there a possibility to build the .so files for Raspberry Pi (ARM architecture)?
    The files included in your ZIP archive don´t work on the Pi, and I guess this is caused by the ARM architecture. The problem is, I´m too stupid to figure that out…. 😦
    Can anybody help me in this case?

  10. DaMaxl says:

    Hi,
    can anybody help me running this project on my Raspberry Pi 2?
    I guess – caused by the ARM architecture of the Pi – it will be necessary to rebuild the native libraries. Actually, the included “libjinput-linux.so” and “libjinput-linux64.so” do not work on the raspberry…

    Would be great if anybody could tell me how to do this!

  11. ruleworld says:

    Works out of the box, great.

  12. Bayu says:

    hi,
    can i change the legth of the axis data? axis value normally minimum at -1.0 . i logitech driving force gt. and to get axisvalueinpercentage maximum only take few degrees to the right of controller. can u help me change the data value?

    • TheUzo007 says:

      Hi,
      I don’t think that you can set max or min value of a component, but you can multiply the value that you got, so that you can get bigger range.

      • Bayu says:

        i want my logitech driving force gt work as like usual on the game and it’s take 900 degrees. if i use your code i only using maximum 90 degrees. so i must change the value of the component. can you suggest something else?

    • TheUzo007 says:

      I don’t know how to solve this. I can’t find anything useful in apidocs. Take a look at apidocs, you can find them in JInputJoystickTestV2\jinput\jinput_coreapi_javadoc\apidocs

  13. ahmed says:

    HI
    i want When I am pressing a button in joystick a specific event occurs ?

    • TheUzo007 says:

      Put if statement into the loop into the if statement wher the buttons are checked. Something like that:
      boolean isItPressed = true;
      if(component.getPollData() == 0.0f)
      isItPressed = false;
      if(buttonIndex == desiredButtonIndex && isItPressed){
      // Do your event.
      }

  14. roal says:

    it’s work fine thank you
    but as you now joystick contain to hand
    i want to determine witch hand it ?

  15. Berni says:

    Great code Sir! This helped me a lot! 1000 thanks for your efforts, especially for this updated version!

  16. atreyu says:

    Very useful post!
    I have had to find and replace an ARM version of libinput-linux.so to run it in a Raspberry Pi 2 B but it works like a charm 🙂
    Thanks a lot, Uzo007.

    • Cameron Nickert says:

      Can you please please please tell me how you did this? I’m writeing a java problem on the raspeberry PI and I need to use joystick input

  17. Luiz Eduardo Costa says:

    Hi there!!

    I’ve used your source code to develop the project of my game and yesterday i showed it on the science fair of my university. Your exemple helped me a lot and i’m so thankful.

    My project is an alien attack, i used the JInput API and Graphic2D. If anyone is interested, the code is on my Git Hub page, feel free to download it or check the source code.

    https://github.com/oluizeduardo/univaders_joystick

  18. Niroj Pariyar says:

    I want to make a joystick app on android that communicates with PC over bluetooth. A server will be running on PC to receive the data. Is there any class or function in JInput library that will recognize the android app as a joystick?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s