8

What library would you recommend to hook up my Xbox 360 controller to Java, and be able to read key inputs into keyPressed Event as a KeyEvent.

So I would like something like this

private class KeyInputHandler extends KeyAdapter {
    public void keyPressed(KeyEvent e) {
    }
}

And I want all the controller presses to go into keyPressed.

I would appreciate it even further if you can provide good libraries for PS3 controllers too.

Little Helper
  • 1,870
  • 3
  • 12
  • 20
Quillion
  • 6,346
  • 11
  • 60
  • 97

2 Answers2

9

The wired XBox 360 controller will present as a joystick in Windows, so a library like JXInput will allow you to accept inputs from it.

Simple example

JXInput site

Corey Cole
  • 977
  • 9
  • 18
  • Perfect! I've been looking for something like this for a while, I thought I would have to write it myself. The only thing is... it says it's 100% Java, but then says it uses the JNI to call non-Java code. Is there any way around this? – John P Feb 28 '14 at 14:11
  • 1
    @JohnP No. Java itself, at its core, also uses non-Java code to realize Java commands on an OS. The thing is that the OS (Windows) does not offer Java access to such things. The Windows libraries are `C`-code (`dll`s). As such, at some point, you will always need to use `JNI` to access the `dll`s from within Java if you want to talk to Windows. – Zabuzard Feb 24 '18 at 01:20
  • 1
    @Zabuza That's very intuitive now that I've spent a couple of years with C and C++, although I think I know what I was hung up on. The JVM already used native libraries because that was its job, leaving the developer to write purely platform-agnostic code. If anything were missing from the JVM to accomplish that, I felt that either the JVM is capable and should be updated or extended, or isn't capable, so with everything available to the JVM I wouldn't be capable either. --- "JNI is not known to be too fast, so the number of calls has been minimized: 1." That alone might have swayed me! – John P Feb 24 '18 at 01:54
3

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;
    }
}
Little Helper
  • 1,870
  • 3
  • 12
  • 20