JInput's getPollData() does not work

541 Views Asked by At

I´m currently dealing with some Problems concerning dual mouse input. I've looked up several libraries and decided that JInput would do best. Although i was able to get a list of all devices plugged into my laptop, i couldn't retrieve any PollData. The following code only produces 0's:

public static void main(String[] args) {

    Controller mouse1 = null;
    Controller[] cs = ControllerEnvironment.getDefaultEnvironment().getControllers();
    for(int i = 0; i < cs.length; i++) {
        if(cs[i].getType() == Type.MOUSE) {
            mouse1 = cs[i];
        }
    }
    mouse1.poll();
    Component[] comps = mouse1.getComponents();
    while(true) {
        mouse1.poll();
        for(int i = 0; i < comps.length; i++) {
            System.out.print(comps[i].getName() + ": ");
            System.out.println(comps[i].getPollData());
        }
    }
}

I also tried out to get KeyBoard-Input with this, same problem. I could get the number of keys, but i was unable to access any information about the key's state. I hope that someone knows how to solve this problem or has an idea what might be causing it.

Thank you in advance, aquatyp.

1

There are 1 best solutions below

0
On

I know this is almost a year old, but for the benefit of anyone who finds this through Google like I did...

Make sure you have an OpenGL display initialized (note the if statement is there just to eliminate the flood of messages):

import net.java.games.input.Controller;
import net.java.games.input.ControllerEnvironment;
import net.java.games.input.Mouse;
import net.java.games.input.RawInputEnvironmentPlugin;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

public class Main
{
 public static void main(String[] args)
 {
  try
  {
   Display.setDisplayMode(new DisplayMode(800,600));
   Display.create();
  }
  catch (LWJGLException e)
  {
   e.printStackTrace();
   System.exit(0);
  }
  
  RawInputEnvironmentPlugin rep = new RawInputEnvironmentPlugin();
  Mouse mouse;
  StringBuilder sb = new StringBuilder();
  while (!Display.isCloseRequested())
  {
   int i = 0;
   for (Controller controller : ControllerEnvironment.getDefaultEnvironment().getControllers())
   {
    if (controller.getType() == Controller.Type.MOUSE)
    {
     //System.out.println(controller.getName() + " | " + controller.getType());
     mouse = (Mouse)controller;
     mouse.poll();
     
     i += 1;
     
     if (mouse.getX().getPollData() > 0.0f || mouse.getY().getPollData() > 0.0f)
     {
      sb.append("[");
      sb.append(i);
      sb.append("] X=");
      sb.append(mouse.getX().getPollData());
      sb.append(" Y=");
      sb.append(mouse.getY().getPollData());
      System.out.println(sb.toString());
      sb.setLength(0);
     }
     
    }
   }
   
   Display.update();
  }
 }
}