简体   繁体   English

Jinput轮询数据永不更改

[英]Jinput Poll Data Never Changes

I am attempting to create a simple test program to get familiar with the JInput Library for another project. 我试图创建一个简单的测试程序来熟悉另一个项目的JInput库。 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) (上述项目本质上是机器人技术)

You have to use an EventQueue 您必须使用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
                }

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. 在构建如上所示的控制台应用程序时,使用JInput始终将鼠标X和y位置的值始终保持为零。 I believe it may be necessary to create a window-focused application of some kind for JInput to work. 我相信可能需要创建某种针对窗口的应用程序以使JInput正常工作。 See here a code example solution and similar issue described. 请参阅此处的代码示例解决方案和描述的类似问题。
code solution 代码解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM