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

 

History

79 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java CI with Maven CodeQL

VController is a helper library for JInput which makes it easy to automatically poll for controller input and trigger events when input is detected.

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.9.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.9.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.9.26"

Example

This rudimentary example will prompt you to select a controller, poll it for events, and print any events to the console.

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

import java.util.Scanner;

public class Example {
    public static void main(String[] args) {
        final var poller = new ControllerPoller(getController());
        poller.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();
        });
        poller.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];
    }
}

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