JInput getPollData() never changes, regardless of input

Hey guys,
Im working on a robotics project and am attempting to have support for controllers via Jinput, however, I am having difficulty getting the polling to work. I am using an Xbox One controller which I have confirmed works the following ways: Played steam games with it, works as expected for all tests provided by jinput, shows up in when I run my code as a connected device. However, when it comes to getting poll data, the values never seem to change. From what I can tell with the debugger, the components all display the “has_pulled” value to be false. I have followed several tutorials (including the one on this forum) and cannot seem to get it working. My values all stay at the same default value of 0.0 or -something for thumbsticks.

Here is my code:

public class ControllerTest {
	
	public static void main(String[] args){
		//System.out.println("Hello World");
		
		Controller[] ca = ControllerEnvironment.getDefaultEnvironment().getControllers();
		Controller gamepad = null;
		Component[] components = null;
		EventQueue eventQueue;
		// Run through the list of available input devices and get the gamepad
		for(int i = 0; i < ca.length; i ++){
			if(ca[i].getType().equals(Controller.Type.GAMEPAD)){  // Snag the first instance of a gamepad
				gamepad = ca[i];
			}
		}
		// Print the name of the controller and its type
		if(gamepad != null){
			System.out.println(gamepad.getName() + ": " + gamepad.getType());
			components = gamepad.getComponents();
			System.out.println("COMPONENTS:");
			for(int i = 0; i < components.length; i ++){
				StringBuffer buffer = new StringBuffer();
				buffer.append("Component #" + i + ": ");  // Get the Component #
				buffer.append("\t" + components[i].getName());  // Get the component name
				// Check if the component is analog or relative
				// Relative relies on its past position for values
				// Analog is a current position
				// Analog absolute is a toggle (think your a,b,y,x button)
				if(components[i].isRelative()){
					buffer.append(" [Relative]");
				}
				else if(components[i].isAnalog()){
					buffer.append(" [Analog]");
				}
				else{
					buffer.append(" [Analog Absolute]");
				}
				System.out.println(buffer.toString());
			}
		}
		else{
			System.out.println("No gamepad connected");
		}
		
		float prevPollData = 100f;
		float curPollData;
		String compName;
		
		while(true){
			
			gamepad.poll();
			
			for(int i = 0; i < components.length; i ++){
				curPollData = components[i].getPollData();
				compName = components[i].getName();
				System.out.println(compName + ": " + curPollData);
			}
			
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

I am quite baffled as to why this works every way but when I use my code, and by all accounts, I believe I am following the correct method for polling and retrieving information. Any help would be greatly appreciated.

Hey, I’m not sure if you got it working, and I’m not sure about your code, but i figured I’d show you mine since it works. I use a 360 controller Here it is:


ControllerPad controller = new ControllerPad();  // ControllerPad is a class I made to handle controller events

public void run() {
                        /**
			 * IF WE ARE USING A CONTROLLER, CHECK IF IT IS DISCONNECTED IF NOT,
			 * GET CONTROLLER CONTROLS
			 */
			if (!hasKeyboard) {
				// GET CURRENT STATE OF CONTROLLER, AND CHECK IF CONTROLLER IS
				// DISCONNECTED
				if (!controller.joystick.pollController()) {
					System.out.println("Controller disconnected!");
				}

				// GET CONTROLLER CONTROLS
				controller.getControls();   //this is the method in ControllerPad that handles all of the controller events
			}
}

public void init() {

		/**
		 * GET LIST OF CONTROLLERS
		 */
		Controller[] ca = ControllerEnvironment.getDefaultEnvironment().getControllers();

		for (int i = 0; i < ca.length; i++) {
			/* GET THE NAME OF THE CONTROLLER */
			System.out.println("Controller " + i + ca[i].getName());
		}

		/**
		 * CHECK IF CONTROLLER WAS FOUND IF NOT, ADD KEY LISTENER
		 */
		if (!controller.joystick.isControllerConnected()) {
			keyboard = new Keyboard(this);
			addKeyListener(keyboard);
			hasKeyboard = true;
			System.out.println("No controller found!");
		}
}




Here is all the code: https://github.com/IanFell/DodgerDog/blob/master/DODGERDOG.java for the main game stuff, but unfortunately the ControllerPad class is not uploaded yet. Let me know if you want to see it and I’ll put it up. It’s basically just a bunch of if/then statements though.

It would be cool to see the ControllerPad class if you could.

Cool, I’ll put it up when I get home from work. I thought I had it here but it looks like I deleted the game files off my work computer.

Ok, nevermind, turns out I had it at work after all. Here it is (some of the formatting is messed up so let me know if you have questions:


public class ControllerPad {
	
    public JInputJoystick joystick 	= new JInputJoystick(Controller.Type.STICK, Controller.Type.GAMEPAD);
	
    public static boolean joystickButton_0;		// X BUTTON
    public static boolean joystickButton_1;		// A BUTTON	
    public static boolean joystickButton_2;		// B BUTTON
    public static boolean joystickButton_3;		// Y BUTTON
    public static boolean joystickButton_6;           // X BOX BUTTON (left trigger?)
    public static boolean joystickButton_7;		// RIGHT TRIGGER
    public static boolean joystickButton_8;		// SELECT BUTTON
    public static boolean joystickButton_9;		// START BUTTON
	
    public ControllerPad(){}
	
     /**
     * getControls() gets called every tick in the game loop
     */
    public void getControls() {
  
    /**
    * Only checks for Type GAMEPAD
    */
    if(joystick.getControllerType().equals(Controller.Type.GAMEPAD)){
        joystickButton_0 		= joystick.getButtonValue(0);
        joystickButton_1 		= joystick.getButtonValue(1);
        joystickButton_2 		= joystick.getButtonValue(2);
        joystickButton_3 		= joystick.getButtonValue(3);
        joystickButton_6 		= joystick.getButtonValue(6);
        joystickButton_7 		= joystick.getButtonValue(7);
			
        float hatSwitchPosition = joystick.getHatSwitchPosition();
    }
	
		if (Float.compare(hatSwitchPosition, Component.POV.UP) == 0 || joystick.getY_LeftJoystick_Percentage() < 30) { }
	
		if (Float.compare(hatSwitchPosition, Component.POV.DOWN) == 0 || joystick.getY_LeftJoystick_Percentage() > 70) { }
	
	        if (Float.compare(hatSwitchPosition, Component.POV.LEFT) == 0 || (joystick.getX_LeftJoystick_Percentage() < 35)) { }
	
		if (Float.compare(hatSwitchPosition, Component.POV.RIGHT) == 0 || (joystick.getX_LeftJoystick_Percentage() > 65)) { }
	
		if (joystickButton_0) { }
	  
      	        else if (joystickButton_1) { }
				
		else if (joystickButton_2) { }
	
		else if (joystickButton_3) { }
	
		if (joystick.getZAxisPercentage() == 0) { }
	
		if (joystickButton_7) { }

		joystick.pollController();

	}

}