LWJGL - How do I find out which button on a controller is pressed?

The code is just me building up on InputExample from the LWJGL Basics 2 (Input) tutorial.

I’m trying to figure out how to get controllers to work and so far all I’ve figured out is how to find out:

  • what controllers are connected
  • what buttons and axis are available
  • if a button is pressed

But I can’t for the life of me figure out how to find out what button is pressed. Can anyone help me out with that? Here’s the code. Sorry if there are some clumsily made stuff in there, I’m just learning the ropes.

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Controller;
import org.lwjgl.input.Controllers;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

public class InputExample {
	
	Controller[] c;
	int gp=-1; //to store controller number of the gamepad
    
    public void start() {
        try {
			Display.setDisplayMode(new DisplayMode(800,600));
			Display.setLocation(43, 12);			
			Display.create();
	    	int cc=0;
	        
	    	try{
	    		Controllers.create();
	    		cc = Controllers.getControllerCount();
	    	} catch (LWJGLException e) {
	            e.printStackTrace();
	            System.exit(0);
	    	}
	    	
	    	c = new Controller[cc];
	    	
	    	for(int i=0; i<cc;i++){
	    		c[i] = Controllers.getController(i);
	    		if(c[i].getName().indexOf("Controller") > -1){
	    			gp=i;
	    		}
	    	}			
    		if(gp==-1){
    			System.out.println("No gamepad controller exists.");	    		
    		}else{
    			int bc = c[gp].getButtonCount();
    			int ac = c[gp].getAxisCount();
    			System.out.println("The controller has "+bc+" buttons and "+ac+" axis.");
    			for(int i=0; i<bc;i++){
    				System.out.println(c[gp].getButtonName(i));
    			}   			
    		}
        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(0);
        }
        
        // init OpenGL here
        
        while (!Display.isCloseRequested()) {
            
            // render OpenGL here
            
            pollInput();
            Display.update();
        }
        
        Display.destroy();
    }
    
    public void pollInput() {
    	
    	if(gp>-1){
    		if(c[gp].isButtonPressed(gp)){
    			System.out.println(c[gp].getButtonName(gp)+" is pressed.");
    		}
    	}
    	
    	while(Controllers.next()){
    		System.out.println("Event came from "+Controllers.getEventSource().getName()+".");
    	}
    	
    	if (Mouse.isButtonDown(0)) {
            int x = Mouse.getX();
            int y = Mouse.getY();
            
            System.out.println("MOUSE DOWN @ X: " + x + " Y: " + y);
        }
        
        if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
            System.out.println("SPACE KEY IS DOWN");
        }
                
        while (Keyboard.next()) {
            if (Keyboard.getEventKeyState()) {
                if (Keyboard.getEventKey() == Keyboard.KEY_A) {
                    System.out.println("A Key Pressed");
                }
                if (Keyboard.getEventKey() == Keyboard.KEY_S) {
                    System.out.println("S Key Pressed");
                }
                if (Keyboard.getEventKey() == Keyboard.KEY_D) {
                    System.out.println("D Key Pressed");
                }
                } else {
                if (Keyboard.getEventKey() == Keyboard.KEY_A) {
                    System.out.println("A Key Released");
                }
                if (Keyboard.getEventKey() == Keyboard.KEY_S) {
                    System.out.println("S Key Released");
                }
                if (Keyboard.getEventKey() == Keyboard.KEY_D) {
                    System.out.println("D Key Released");
                }
            }
        }
    }
    
    public static void main(String[] argv) {
        InputExample inputExample = new InputExample();
        inputExample.start();
    }
}

Hmmm… couldn’t figure it out.

Realized I could implement what I needed to do (with regards to controls) with JInput better than with LWJGL.

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import net.java.games.input.*;

public class InputExample {
	
	Controller[] ca;
	int gp=-1; //to store controller number of the gamepad
    
    public void start() {
        try {
			Display.setDisplayMode(new DisplayMode(800,600));
			Display.setLocation(43, 12);			
			Display.create();
	    	ca = ControllerEnvironment.getDefaultEnvironment().getControllers();	    	
	    	for(int i =0;i<ca.length;i++){

	             /* Get the name of the controller */
	             System.out.println(ca[i].getName());
	             System.out.println("Type: "+ca[i].getType().toString());	             
	             if(ca[i].getType().toString().indexOf("Gamepad")>-1){
	            	 gp=i;

		             /* Get this controllers components (buttons and axis) */
		             Component[] components = ca[i].getComponents();
		             System.out.println("Component Count: "+components.length);
		             for(int j=0;j<components.length;j++){
		                 
		                 /* Get the components name */
		                 System.out.println("Component "+j+": "+components[j].getName());
		                 System.out.println("    Identifier: "+ components[j].getIdentifier().getName());
		                 System.out.print("    ComponentType: ");
		                 if (components[j].isRelative()) {
		                     System.out.print("Relative");
		                 } else {
		                     System.out.print("Absolute");
		                 }
		                 if (components[j].isAnalog()) {
		                     System.out.print(" Analog");
		                 } else {
		                     System.out.print(" Digital");
		                 }
		             }
		             System.out.println("");
	             }
	    	 }
        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(0);
        }
        
        // init OpenGL here
        
        while (!Display.isCloseRequested()) {
            
            // render OpenGL here
            
            pollInput();
            Display.update();
        }
        
        Display.destroy();
    }
    
    public void pollInput() {
    	
    	ca[gp].poll();
    	EventQueue queue = ca[gp].getEventQueue();
   	 	Event event = new Event();

   	 	while(queue.getNextEvent(event)) {
	   	 	StringBuffer buffer = new StringBuffer(ca[gp].getName());
	        buffer.append(" at ");
	        buffer.append(event.getNanos()).append(", ");
	        Component comp = event.getComponent();
	        buffer.append(comp.getName()).append(" changed to ");
	        float value = event.getValue(); 
	        if(comp.isAnalog()) {
	           buffer.append(value);
	        } else {
	           if(value==1.0f) {
	              buffer.append("On");
	           } else {
	              buffer.append("Off");
	           }
	        }
	        System.out.println(buffer.toString());
   	 	}
    	
    	if (Mouse.isButtonDown(0)) {
            int x = Mouse.getX();
            int y = Mouse.getY();
            
            System.out.println("MOUSE DOWN @ X: " + x + " Y: " + y);
        }
        
        if (Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
            System.out.println("SPACE KEY IS DOWN");
        }
                
        while (Keyboard.next()) {
            if (Keyboard.getEventKeyState()) {
                if (Keyboard.getEventKey() == Keyboard.KEY_A) {
                    System.out.println("A Key Pressed");
                }
                if (Keyboard.getEventKey() == Keyboard.KEY_S) {
                    System.out.println("S Key Pressed");
                }
                if (Keyboard.getEventKey() == Keyboard.KEY_D) {
                    System.out.println("D Key Pressed");
                }
                } else {
                if (Keyboard.getEventKey() == Keyboard.KEY_A) {
                    System.out.println("A Key Released");
                }
                if (Keyboard.getEventKey() == Keyboard.KEY_S) {
                    System.out.println("S Key Released");
                }
                if (Keyboard.getEventKey() == Keyboard.KEY_D) {
                    System.out.println("D Key Released");
                }
            }
        }
    }
    
    public static void main(String[] argv) {
        InputExample inputExample = new InputExample();
        inputExample.start();
    }
}