LibGDX Game Controller

libgdx game development java

As with any low level code, eventually you get tired of working with it and wrap up common behavior. In my case I wanted to design for a few things:

  1. A native interface for simultaneously handling both keyboard and logitech controller input.
  2. A button mapper to easily allow for custom mappings.
  3. Record time when buttons are pressed to allow for easier support for capturing button combinations.

Get Library

GDX-Controller is now hosted in Maven Central here:


The GitHub repository will be maintained here.


We'll start with our basic definition of buttons. This could be expanded depending on needs. Given my love of classic SNES style games, this is sufficient.

A general interface for button enum classes

public interface Controls {

A simple SNES-like control schema (with L2,R2)

public enum GameControls implements Controls {




Now that we have our Button "interface" defined, we need to create mappings to the internal integer key codes and joystick axi. The reason this is important is because different input method's keys may map to different values. The key codes for a logitech controller is significantly different than a computer's keyboard key codes. This abstraction also allows a user to easily map keys differently, for example, inverting up and down, or jump and run.

The class for handling button mappings

public class ButtonMapper<V extends Controls> {

    private final ObjectMap<V, Integer> mapping = new ObjectMap<>();

    public ButtonMapper() {}

    public void map(V control, Integer mapping) {
        this.mapping.put(control, mapping);

    public final Integer get(V control) {
        return mapping.get(control, -1);


The class for handling axis mappings

public class AxisMapper<V> {

    protected final ObjectMap<V, Axis> mapping = new ObjectMap<>();

    public AxisMapper() {}

    public void map(V control, Axis mapping) {
        this.mapping.put(control, mapping);

    public Axis get(V control) {
        return mapping.get(control, null);


Now lets start building our controller interface and implementations. Unfortunately this implementation does not directly support input combinations yet, but that will be supported in the near future.

public abstract class Controller<V extends Controls> {

    private ObjectMap<V, Long> lastPressed = new ObjectMap<>();

    protected ButtonMapper<V> buttonMapper;

    protected AxisMapper<V> axisMapper;

    public abstract boolean isPressed(V control);

    public long when(V control) {
        return lastPressed.get(control, 0L);

    public void record(V control) {
        lastPressed.put(control, TimeUtils.millis());


Our keyboard input implementation

public class KeyboardController<V extends Controls> extends Controller<V> {

    public KeyboardController(final ButtonMapper<V> buttonMapper) {
        super.buttonMapper = buttonMapper;

    public boolean isPressed(V control) {
        final boolean pressed = Gdx.input.isKeyPressed(buttonMapper.get(control));
        if (pressed) { record(control); }
        return pressed;


Our Logitech controller input implementation. It is very similar to the keyboard implementation with the exception of the handling of it's joystick.

public class LogitechController<V extends Controls> extends Controller<V> {

    private final com.badlogic.gdx.controllers.Controller controller;

    public LogitechController(final int controllerNumber,
                              final ButtonMapper<V> buttonMapper,
                              final AxisMapper axisMapper) {
        super.buttonMapper = buttonMapper;
        super.axisMapper = axisMapper;

        if(Controllers.getControllers().size < (controllerNumber + 1)) {
            controller = null;
        controller = Controllers.getControllers().get(controllerNumber);

    public boolean isPressed(V control) {
        if (controller == null) { return false; }

        boolean pressed = false;
        if (axisMapper.get(control) != null) {
            final Axis axis = this.axisMapper.get(control);
            if (axis.threshold < 0) {
                pressed = controller.getAxis( < axis.threshold;
            if (axis.threshold > 0) {
                pressed = controller.getAxis( > axis.threshold;
        else {
            pressed = controller.getButton(buttonMapper.get(control));

        if (pressed) { record(control); }
        return pressed;

Now lets bring it all together and wrap our input methods in a "multiplex" controller that is capable of simultaneously supporting configureable keyboard and logitech input without any hassle to either the programmer or the game player. Note how MultiController also implements the Controller interface just as KeyboardController and LogitechController.

public class MultiplexedController<V extends Controls> extends Controller<V> {

    private final Controller[] controllers;

    public MultiplexedController(final Controller... controllers) {
        this.controllers = controllers;

    public boolean isPressed(final V control) {
        for (Controller controller : controllers) {
            if (controller.isPressed(control)) {
                return true;
        return false;

    public long whenPressed(final V control) {
        for (Controller controller : controllers) {
            final long when = controller.when(control);
            if (when > 0) {
                return when;
        return 0L;


Using MultiController in your game should now only require a minimum of code. You must first configure your controller, and then you can use it however you please. Below we will create a sample controller factory.

public class MyControllerFactory {

    public static MultiplexedController<GameControls> buildMultiController() {
        return new MultiplexedController<>(buildKeyboard(), buildLogitech());

    public static KeyboardController<GameControls> buildKeyboard() {
        final ButtonMapper<GameControls> buttonMapper = new ButtonMapper<>();, Input.Keys.UP);, Input.Keys.DOWN);, Input.Keys.LEFT);, Input.Keys.RIGHT);, Input.Keys.ENTER);, Input.Keys.SHIFT_RIGHT);, Input.Keys.Z);, Input.Keys.X);, Input.Keys.A);, Input.Keys.S);, Input.Keys.Q);, Input.Keys.W);, Input.Keys.C);, Input.Keys.D);

        return new KeyboardController<>(buttonMapper);

    public static LogitechController<GameControls> buildLogitech() {
        final ButtonMapper<GameControls> buttonMapper = new ButtonMapper<>();, 9);, 8);, 1);, 2);, 0);, 3);, 4);, 6);, 5);, 7);

        final AxisMapper<GameControls> axisMapper = new AxisMapper<>();, new Axis(1, -0.75f));, new Axis(1, 0.75f));, new Axis(0, -0.75f));, new Axis(0, 0.75f));

        return new LogitechController<>(0, buttonMapper, axisMapper);


In the beginning of your game screen create a multi controller with the following code.

private Controller<GameControls> controller = MyControllerFactory.buildMultiController();

A sample handle input method for a space shooter.

private void handleInput(final float deltaTime) {
    // process user input
    ship.vx = 0;
    ship.vy = 0;

    if (controller.isPressed(GameControls.DPAD_LEFT)) {
        ship.vx = -deltaTime;
    if (controller.isPressed(GameControls.DPAD_RIGHT)) {
        ship.vx = deltaTime;
    if (controller.isPressed(GameControls.DPAD_UP)) {
        ship.vy = deltaTime;
    if (controller.isPressed(GameControls.DPAD_DOWN)) {
        ship.vy = -deltaTime;
    if (controller.isPressed(GameControls.L1)) {
    if (controller.isPressed(GameControls.R1)) {
    if (controller.isPressed(GameControls.SELECT)) {
        game.setScreen(new GameScreen(game));
    float[] v = Vector.unit2d(ship.vx, ship.vy);
    ship.vx = v[0] * ship.speed;
    ship.vy = v[1] * ship.speed;

    if (controller.isPressed(GameControls.A)) {

I hope this is better than manually referencing Gdx.input calls directly!