Jinput轮询数据永不更改 [英] Jinput Poll Data Never Changes

查看:102
本文介绍了Jinput轮询数据永不更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个简单的测试程序来熟悉另一个项目的JInput库.我已经使用所有提供的测试类对控制器进行了测试,并且它可以按预期工作.但是,当我尝试轮询控制器时,无论我输入什么,所有值都保持不变.这是我正在使用的代码:

public class ControllerTest {

}

我一直在尝试在线查找答案,但是该库的文档记录不是很好,似乎通常包装在其他专门制作游戏的库中. (上述项目本质上是机器人技术)

解决方案

在构建如上所示的控制台应用程序时,我始终使用JInput来获得鼠标X和y位置的零值.我认为可能需要创建某种以窗口为中心的应用程序,以使JInput正常工作.请参阅此处的代码示例解决方案和描述的类似问题.
代码解决方案

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)

解决方案

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

这篇关于Jinput轮询数据永不更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆