Skip to content

A helper library for JInput which makes it easy to automatically poll for controller input, connection, and disconnection events and notify listeners when they occur.

License

Notifications You must be signed in to change notification settings

Valkryst/VController

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java CI with Maven CodeQL

VController is a helper library for JInput with the following features:

  • Automatic polling of controller input events VIA ControllerPoller.
  • Automatic detection of controller connection and disconnection events VIA HotSwapPoller.
    • As JInput does not natively support hot-swapping, this library uses a polling approach to detect it. The downside to this approach is that JInput may print messages to System.err on every poll.
    • This will cause a resource leak on Windows. A solution is being discussed here.
      • To mitigate this issue, I suggest only enabling the HotSwapPoller when showing the user a list of controllers to select from. Reducing the polling rate will also help as each poll creates a "dead" thread which takes up system resources.
  • Interfaces to listen to controller and hot-swap events VIA ControllerListener and HotSwapListener.

This library assumes that you have already properly configured JInput and that it is working.

Table of Contents

Installation

VController is hosted on the JitPack package repository which supports Gradle, Maven, and sbt.

Gradle Gradle

Add JitPack to your build.gradle at the end of repositories.

allprojects {
	repositories {
		...
		maven { url 'https://jitpack.io' }
	}
}

Add VController as a dependency.

dependencies {
	implementation 'com.github.Valkryst:VController:2023.10.26'
}

Maven Maven

Add JitPack as a repository.

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

Add VController as a dependency.

<dependency>
    <groupId>com.github.Valkryst</groupId>
    <artifactId>VController</artifactId>
    <version>2023.10.26</version>
</dependency>

Scala SBT Scala SBT

Add JitPack as a resolver.

resolvers += "jitpack" at "https://jitpack.io"

Add VController as a dependency.

libraryDependencies += "com.github.Valkryst" % "VController" % "2023.10.26"

Example

Controller Polling

This example prompts you to select a controller, then it continuously polls the controller for events and prints them to the console.

import net.java.games.input.Controller;
import net.java.games.input.ControllerEnvironment;

import java.util.Scanner;

public class ControllerExample {
    public static void main(String[] args) {
        final var controllerPoller = new ControllerPoller(getController());
        controllerPoller.addListener(event -> {
            final var component = event.getComponent();
            System.out.println(component.getName());
            System.out.println(component.getIdentifier());
            System.out.println(component.getDeadZone());
            System.out.println(component.getPollData());
            System.out.println();
        });
        controllerPoller.start(16);

        System.out.println("\nPolling for events. Try moving the controller's joysticks and pressing its buttons.");
    }

    private static ControllerEnvironment getEnvironment() {
        final var environment = ControllerEnvironment.getDefaultEnvironment();

        if (!environment.isSupported()) {
            throw new IllegalStateException("Controller environment is not supported.");
        }

        return environment;
    }

    private static Controller getController() {
        final var environment = getEnvironment();
        final var controllers = environment.getControllers();

        System.out.println("Detected Controllers");
        for (int i = 0 ; i < controllers.length ; i++) {
            System.out.println("\t" + i + ": " + controllers[i].getName());
        }

        System.out.print("\nEnter a number to select a controller: ");
        final var scanner = new Scanner(System.in);
        final var index = scanner.nextInt();
        System.out.println("\nYou selected: " + controllers[index].getName());

        return controllers[index];
    }
}

Hot Swap Polling

This example prints a message to the console when a controller is connected or disconnected.

import net.java.games.input.Controller;

public class HotSwapExample {
    public static void main(final String[] args) {
        final var hotswapPoller = HotSwapPoller.getInstance();
        hotswapPoller.addListener(new HotSwapListener() {
            @Override
            public void controllerAdded(Controller controller) {
                System.out.println("Added: " + controller.getName());
            }

            @Override
            public void controllerRemoved(Controller controller) {
                System.out.println("Removed: " + controller.getName());
            }
        });

        hotswapPoller.start();

        System.out.println("Polling for hot-swaps. Try adding or removing a controller.");
    }
}

Credits & Inspiration

About

A helper library for JInput which makes it easy to automatically poll for controller input, connection, and disconnection events and notify listeners when they occur.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Languages