Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (527)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (594)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  LWJGL - How do I find out which button on a controller is pressed?  (Read 1481 times)
0 Members and 1 Guest are viewing this topic.
Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Posted 2013-04-27 02:49:31 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
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();
    }
}

Offline heisenbergman

JGO Coder


Medals: 14


L___ o_ G___ a__ P___


« Reply #1 - Posted 2013-04-27 03:29:42 »

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.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
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();
    }
}

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

PocketCrafter7 (13 views)
2014-11-28 16:25:35

PocketCrafter7 (9 views)
2014-11-28 16:25:09

PocketCrafter7 (10 views)
2014-11-28 16:24:29

toopeicgaming1999 (76 views)
2014-11-26 15:22:04

toopeicgaming1999 (66 views)
2014-11-26 15:20:36

toopeicgaming1999 (15 views)
2014-11-26 15:20:08

SHC (29 views)
2014-11-25 12:00:59

SHC (27 views)
2014-11-25 11:53:45

Norakomi (32 views)
2014-11-25 11:26:43

Gibbo3771 (28 views)
2014-11-24 19:59:16
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!