首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jinput轮询数据从不更改

Jinput轮询数据从不更改
EN

Stack Overflow用户
提问于 2016-11-23 01:25:50
回答 2查看 376关注 0票数 0

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

public class ControllerTest {

代码语言:javascript
复制
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();
        }
    }
}

}

我一直试图在网上寻找答案,但这个库并没有很好的文档记录,而且似乎通常被包装在其他专门制作游戏的库中。(前面提到的项目本质上是机器人的)

EN

回答 2

Stack Overflow用户

发布于 2018-05-31 04:01:58

在构建如上所示的控制台应用程序时,我一直使用JInput为鼠标X和Y位置获取零值。我认为可能有必要创建某种以窗口为中心的应用程序,以便让JInput正常工作。请看这里描述的代码示例解决方案和类似的问题。

code solution

票数 0
EN

Stack Overflow用户

发布于 2017-03-11 06:41:44

您必须使用EventQueue

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40748032

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档