Jinput民意调查数据永不改变

时间:2016-11-22 17:25:50

标签: java input controller robotics jinput

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

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();
        }
    }
}

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(); } } }

我一直试图在网上找到答案,但这个图书馆的文档记录不是很好,而且似乎通常包含在其他特定于制作游戏的库中。 (上述项目本质上是机器人)

2 个答案:

答案 0 :(得分:0)

您必须使用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
                }

答案 1 :(得分:0)

在构建如上所示的控制台应用程序时,我一直只使用JInput为鼠标X和y位置获取零值。我相信可能有必要为JInput创建一个以窗口为中心的应用程序来工作。请参阅此处的代码示例解决方案和所述的类似问题 code solution