Know a good Java library to read from a Gamepad or Joystick?

I’m working on a program for our AUV (Not for FRC) and I need to be able to get values from a gamepad/joystick in a java program.
So far we’ve tried LWJGL and JInput, and both have issues with detecting if a controller disconnected (they will print an error to command line, but not actually throw an error).
It would also be nice (but not necessary) if the library was packed up into a .jar, so that we wouldn’t need to install .dll’s for each computer the program would run on.

Thanks in advance for all replies.

In JInput, the comment for net.java.games.input.Controller.poll() says:

/**
 * Polls axes for data.  Returns false if the controller is no longer valid.

In LWJGL’s GLFW library, there’s

bool present = org.lwjgl.glfw.GLFW.[glfwJoystickPresent](http://javadoc.lwjgl.org/org/lwjgl/glfw/GLFW.html#glfwJoystickPresent(int))(GLFW_JOYSTICK_1) == org.lwjgl.opengl.GL11.GL_TRUE;

Yeah. I would just poll the controller with JInput and if you need an exception thrown, do it upon receiving ‘false’

I’m pretty sure LWJGL uses jinput behind the scenes for input handling (at least it used to), so you will likely get the same behavior with both libraries.

I wrote an application a while back that used some really hacky code to re-initialize Jinput to rescan for joysticks, which might be somewhat helpful. IIRC, the controller listener hooks worked correctly on Windows, but that hack was necessary on Linux.

It’s been a while, so I forget how I dealt with the native libraries, but that application might also be able to help with that.

Here’s one I started using. I really like it so far and does everything that you require.

-Andres

Weird. I tried that in LWJGL, but it didn’t work. I assumed it wouldn’t work in JInput, but looks like it does.

LWJGL’s docs

How would you check if the controller has reconnected? Would the .poll work again?
What if you connected a new joystick? How would you rebuild the list attached gamepads, if, say, you started the program and then connected a controller? IIRC, both JInput and LWJGL only build the list the first time you call it, and will give you only that list from that point on.

Perhaps LWJGL is using an old version of JInput.

I’m not sure. Why don’t you try it?

The GitHub version of JInput allows you to register listeners for controller state changes. That would probably be the most straight forward way.

I assume if JInput is able to fire events on controller state changes that it’s also maintaining an updated list, but I haven’t verified this.