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?
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.
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.