[英]Jinput - Get Mouse Location
我正在尝试使用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从鼠标获取鼠标数据?
JInput处于较低级别,您正在混淆窗口指针和鼠标。 鼠标只是相对轴> 2的设备。 每次轮询后或在每个事件中的值都不是像素数或位置,它仅是从其先前值开始的大致抽象单位的偏移量。 对于相同的物理距离更改,某些鼠标报告较大的值,因此必须缩放它,这就是使用DirectX鼠标(也是相对轴设备)的游戏具有鼠标缩放滑块的原因。
从JInput github JInput @ GitHub下载并按照其示例ReadFirstMouse.java
创建类似于您的主函数后,鼠标x和y的增量也只有零。
我最终发现了一项围绕创建JavaFX应用程序的工作。 我还发现,使用JFrame应用程序JFrame解决方案可以解释和解决相同的零问题。 因此,这可能是Windows系统特别存在的一个问题,因为我也在使用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}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.