There is an open-source project called Jamepad.
Download the project and add it to the dependencies of your project.
It works out-of-the-box with my wireless Xbox 360 controller.
I made a game with the following input types:
public enum InputAction {
MOVE_UP, MOVE_DOWN, MOVE_LEFT, MOVE_RIGHT
}
The following class will then handle your controller and convert the input to your own internal representation.
public class GamepadInput {
private final ControllerManager controllers;
public GamepadInput() {
controllers = new ControllerManager();
controllers.initSDLGamepad();
}
Set<InputAction> actions() {
ControllerState currState = controllers.getState(0);
if (!currState.isConnected) {
return Collections.emptySet();
}
Set<InputAction> actions = new HashSet<>();
if (currState.dpadLeft) {
actions.add(InputAction.MOVE_LEFT);
}
if (currState.dpadRight) {
actions.add(InputAction.MOVE_RIGHT);
}
if (currState.dpadUp) {
actions.add(InputAction.MOVE_UP);
}
if (currState.dpadDown) {
actions.add(InputAction.MOVE_DOWN);
}
return actions;
}
}