Jinput Poll Data Never Changes

耗尽温柔 提交于 2020-01-26 04:06:10

问题


I am attempting to create a simple test program to get familiar with the JInput Library for another project. I have tested my controller with the all of the provided test classes and it works as expected. However, when I attempt to poll the controller, all values remain unchanged regardless of my input. Here is the code I am working with:

public class ControllerTest {

public static void main(String[] args){
    //System.out.println("Hello World");

    Controller[] ca = ControllerEnvironment.getDefaultEnvironment().getControllers();
    Controller gamepad = null;
    Component[] components = null;
    EventQueue eventQueue;
    // Run through the list of available input devices and get the gamepad
    for(int i = 0; i < ca.length; i ++){
        if(ca[i].getType().equals(Controller.Type.GAMEPAD)){
            gamepad = ca[i];
        }
    }
    // Print the name of the controller and its type
    if(gamepad != null){
        System.out.println(gamepad.getName() + ": " + gamepad.getType());
        components = gamepad.getComponents();
        System.out.println("\tComponents:");
        for(int i = 0; i < components.length; i ++){
            System.out.println("\t\tComponent #" + i + ": " + components[i].getName() + "\n\t\t\tIs Relative: "
                    + components[i].isRelative());
        }
    }
    else{
        System.out.println("No gamepad connected");
    }

    while (true){
        // If we have no gamepad connected, exit
        if(gamepad == null){
            System.out.println("No Gamepad detected, exiting...");
            System.exit(0);
        }
        // Poll controller
        gamepad.poll();
        Component[] comp = gamepad.getComponents();

        for(int i = 0; i < comp.length; i ++){
            StringBuffer buffer = new StringBuffer();
            buffer.append(comp[i].getName());
            buffer.append(", Value: " + comp[i].getPollData());
            System.out.println(buffer.toString());
        }
        try{
            Thread.sleep(20);  // Sleep before polling again
        }
        catch(InterruptedException e){
            e.printStackTrace();
        }
    }
}

}

I have been trying to find an answer online, but this library is not very well documented and seems to usually be wrapped in other libraries specific for making games. (The aforementioned project is robotic in nature)


回答1:


I was consistently getting only zero values using JInput for mouse X and y locations when building a console application like you have shown above. I believe it may be necessary to create a window-focused application of some kind for JInput to work. See here a code example solution and similar issue described.
code solution




回答2:


You have to use an EventQueue

player.poll();
        EventQueue queue = player.getEventQueue();
        Event event = new Event();
        while (queue.getNextEvent(event)) {
            Component comp = event.getComponent();
            if (comp.getIdentifier() == Component.Identifier.Button._6){
                if (comp.getPollData() == 1){
                    example
                }


来源:https://stackoverflow.com/questions/40748032/jinput-poll-data-never-changes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!