Jinput - 获取鼠标位置

时间:2015-10-12 22:44:18

标签: java jinput

我正在尝试使用Jinput打印出鼠标位置:

public static void main(String[] args) {
    input = new InputManager();

    while (true) {
        for (Mouse mouse : input.getMice()) {
            mouse.poll();
            System.out.println("Mouse X: " + mouse.getX().getPollData());
            System.out.println("Mouse Y: " + mouse.getY().getPollData());
            System.out.println("---------");
        }
        try {
            Thread.sleep(100);
        } catch (Exception e) {
            // DO NOTHING < BAD
        }
    }
}

这是我的InputManager,在初始化时扫描所有输入设备,并将所有鼠标分成单独的列表:

public class InputManager {
    public ArrayList<Mouse> mice;

    public InputManager() {
        mice = new ArrayList<Mouse>();
        Controller[] inputs = ControllerEnvironment.getDefaultEnvironment()
            .getControllers();
        for (int i = 0; i < inputs.length; i++) {
            Mouse mouse;
            if (inputs[i].getType() == Controller.Type.MOUSE) {
                mouse = (Mouse) inputs[i];
                mice.add(mouse);
            }
        }
        System.out.println("Discovered " + mice.size() + " mice.");
    }

    public ArrayList<Mouse> getMice() {
        return mice;
    }
}

对于x和y,正在打印的信息始终为0。我在Windows 10上运行它,这会导致任何问题吗?如何使用Jinput从鼠标中获取鼠标数据?

2 个答案:

答案 0 :(得分:1)

JInput是较低级别,您会混淆窗口指针和鼠标。鼠标只是具有> 2相对轴的设备。每次轮询之后或每次事件中的值不是像素数或位置,它只是从它的先前值以大致抽象单位的偏移量。有些鼠标报告相同物理距离变化的值越大,因此您必须对其进行缩放,这就是使用directx鼠标(也是相对轴设备)的游戏具有鼠标缩放滑块的原因。

答案 1 :(得分:0)

从JInput github JInput @ GitHub下载后,我也只获得了鼠标x和y增量的零,并按照他们的示例ReadFirstMouse.java创建了与你的主函数非常相似的主函数。

我最终发现了一项涉及创建JavaFX应用程序的工作。我还发现使用JFrame应用程序JFrame solution解释并解决了这个相同的零问题。所以这可能是Window系统的一个问题,因为我也使用Windows 7,但我不确定。

这是一个Kotlin w / TornadoFx解决方案,但它可能很容易转换为Java / JavaFx。

import javafx.animation.AnimationTimer
import javafx.geometry.Pos
import net.java.games.input.Controller
import net.java.games.input.ControllerEnvironment
import tornadofx.*

class JInputView : View("----------JInput Demo---------") {
val mice = getMice()
val labels=mice.map{label(it.name)}

override val root = vbox(20, Pos.BASELINE_LEFT) {
    setPrefSize(400.0,100.0)
    mice.forEachIndexed { i, m ->
        hbox {
            label(m.name + "  ->")
            children.add(labels[i])
        }
    }
}
val timer = object : AnimationTimer() {
    override fun handle(now: Long) {
        mice.forEachIndexed {i,it->
            it.poll() // Poll the controller
            // Get the axes
            val xComp = it.getComponent(net.java.games.input.Component.Identifier.Axis.X)
            val yComp = it.getComponent(net.java.games.input.Component.Identifier.Axis.Y)
            labels[i].text = "x,y= %f, %f".format(xComp.pollData,yComp.pollData)
        }
    }
}

init { timer.start() }
}

fun getMice() : List<Controller> {
    //don't forget to set location for DLL, or use command line option: -Djava.library.path="
    System.setProperty( "java.library.path", "C:/Your Directory where Dll is present" );

    /* Get the available controllers */
    val controllers = ControllerEnvironment.getDefaultEnvironment().controllers
    println("number controllers %d".format(controllers.size))
    return controllers.filter{it.type==Controller.Type.MOUSE}
}