I am attempting to print out the mouse location using 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
}
}
}
Here is my InputManager, which upon initialization scans for all the input devices, and separates all the mice into a separate list:
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;
}
}
The information that is being printed out is always 0 for both x and y. I am running this on windows 10, does that cause any issues? How do I get the mouse data from the mouse using Jinput?
JInput is lower level, you are confusing a window pointer, and a mouse. The mouse is just a device with >2 relative axis. The values after each poll or in each event are not a number of pixels, or a position, it is just an offset in roughly abstract units from it's previous value. Some mice report larger values for the same amount of physical distance changed, so you have to scale it, which is why games that use the directx mouse (also a relative axis device) have a mouse scale slider.
I was also getting only zeros for mouse x and y increments after downloading from JInput github JInput @ GitHub and creating a main function much like yours by following their example ReadFirstMouse.java
.
I eventually discovered a work around involving creating a JavaFX application instead. I also found this same zeros problem explained and solved with a JFrame application JFrame solution. So this may be a problem specifically on Window's systems as I'm also using Windows 7, but I'm not sure.
Here is a Kotlin w/TornadoFx solution, but it perhaps easy to convert to 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/jinput-get-mouse-location