我正在尝试使用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从鼠标中获取鼠标数据?
发布于 2015-10-14 21:16:53
JInput级别较低,您混淆了窗口指针和鼠标。鼠标只是一个相对轴>2的设备。每次投票后或在每个事件中的值都不是若干像素,也不是一个位置,它只是一个从它以前的值中以大致抽象单位表示的偏移量。有些鼠标报告的物理距离值变化较大,因此必须进行缩放,这就是为什么使用directx鼠标(也是相对轴设备)的游戏有鼠标刻度滑块的原因。
发布于 2018-05-30 19:00:42
在从JInput github JInput @ GitHub下载并创建一个与您的函数非常相似的主函数之后,我还得到了鼠标x和y增量的零,方法是遵循它们的示例ReadFirstMouse.java。
我最终发现了一项涉及创建JavaFX应用程序的工作。我还发现,用JFrame应用程序JFrame解解释并解决了同样的零问题。因此,这可能是一个问题,特别是在窗口的系统,因为我也使用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}
}https://stackoverflow.com/questions/33091388
复制相似问题