Hi !
I’ve been using the getDefaultEnvironment().getControllers() to get the controllers before and everyone was fine (either using event to read the input or use the polldata of each component). But since I want to verify if the controller is plugged in or not, it seems that I need to use the DirectAndRawInputEnvironmentPlugin() instead. So I did a quick switch and it’s not working. My event queue is never triggered and the polldata of each component is always 0. Here the code of the two solutions I tried:
import javax.swing.Timer;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import net.java.games.input.*;
import net.java.games.input.Component;
import net.java.games.input.Component.Identifier;
import net.java.games.input.Controller;
import net.java.games.input.ControllerEnvironment;
import net.java.games.input.DirectAndRawInputEnvironmentPlugin;
import net.java.games.input.Event;
import net.java.games.input.EventQueue;
public class detect_all_bouttons {
boutton_control_selection selection;
Timer le_timer;
Controller[] controllers;
public detect_all_bouttons(boutton_control_selection la_section){
selection = la_section;
ActionListener actionlistener= new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Controller[] controllers_temp;
DirectAndRawInputEnvironmentPlugin directEnv = new DirectAndRawInputEnvironmentPlugin();
if (directEnv.isSupported()) {
controllers_temp = directEnv.getControllers();
} else {
controllers_temp = ControllerEnvironment.getDefaultEnvironment().getControllers();
}
if(controllers == null)
{
if (directEnv.isSupported()) {
controllers = directEnv.getControllers();
} else {
controllers = ControllerEnvironment.getDefaultEnvironment().getControllers();
}
}
else if(controllers.length != controllers_temp.length)
{
if (directEnv.isSupported()) {
controllers = directEnv.getControllers();
} else {
controllers = ControllerEnvironment.getDefaultEnvironment().getControllers();
}
}
StringBuffer buffer = null;
for (int i = 0; i < controllers.length; i++) {
/* Remember to poll each one */
if(controllers[i].getType()!=Controller.Type.MOUSE)
{
controllers[i].poll();
Component[] components = controllers[i].getComponents();
for(int x=0; x < components.length; x++) {
controllers[i].poll();
//always getting a 0 for PollData even though the key is pressed
if(components[x].getPollData() > 0)
{
selection.effacer_le_input(controllers[i].getName(), components[x].getName(), i);
le_timer.stop();
return;
}
}
/* Get the controllers event queue */
controllers[i].poll();
EventQueue queue = controllers[i].getEventQueue();
/* Create an event object for the underlying plugin to populate */
Event event = new Event();
//System.out.println(queue);
/* For each object in the queue */
while (queue.getNextEvent(event)) {
//The application never goes into the loop
/*
* Create a strug buffer and put in it, the controller name,
* the time stamp of the event, the name of the component
* that changed and the new value.
*
* Note that the timestamp is a relative thing, not
* absolute, we can tell what order events happened in
* across controllers this way. We can not use it to tell
* exactly *when* an event happened just the order.
*/
buffer = new StringBuffer(controllers[i].getName());
buffer.append(" : ");
Component comp = event.getComponent();
float value = event.getValue();
boolean stick_dead_zone = true;
String texte_manette = null;
if(comp.getName().equals("X Axis") || comp.getName().equals("Y Axis"))
{
String le_texte = "left joystick";
if(value > 0.5 && comp.getName().equals("X Axis"))
{
texte_manette = "Right on the left joystick";
}
else if(value < -0.5 && comp.getName().equals("X Axis"))
{
texte_manette = "Left on the left joystick";
}
else if(value > 0.5 && comp.getName().equals("Y Axis"))
{
texte_manette = "Down on the left joystick";
}
else if(value < -0.5 && comp.getName().equals("Y Axis"))
{
texte_manette = "Up on the left joystick";
}
else
{
stick_dead_zone = false;
}
}
else if(comp.getName().equals("X Rotation") || comp.getName().equals("Y Rotation"))
{
String le_texte = "right joystick";
if(value > 0.5 && comp.getName().equals("X Rotation"))
{
texte_manette = "Right on the right joystick";
}
else if(value < -0.5 && comp.getName().equals("X Rotation"))
{
texte_manette = "Left on the right joystick";
}
else if(value > 0.5 && comp.getName().equals("Y Rotation"))
{
texte_manette = "Down on the right joystick";
}
else if(value < -0.5 && comp.getName().equals("Y Rotation"))
{
texte_manette = "Up on the right joystick";
}
else
{
stick_dead_zone = false;
}
}
else if((comp.getName().equals("Z Rotation") && controllers[i].getName().contains("Controller") != true) || (comp.getName().equals("Z Axis") && controllers[i].getName().contains("Controller") != true))
{
String le_texte = "right joystick";
if(value > 0.5 && comp.getName().equals("Z Axis"))
{
texte_manette = "Right on the right joystick";
}
else if(value < -0.5 && comp.getName().equals("Z Axis"))
{
texte_manette = "Left on the right joystick";
}
else if(value > 0.5 && comp.getName().equals("Z Rotation"))
{
texte_manette = "Down on the right joystick";
}
else if(value < -0.5 && comp.getName().equals("Z Rotation"))
{
texte_manette = "Up on the right joystick";
}
else
{
stick_dead_zone = false;
}
}
else if(comp.getName().equals("Hat Switch"))
{
Identifier identifier = Component.Identifier.Axis.POV;
if(controllers[i].getComponent(identifier).getPollData() == 1)
{
texte_manette = "Left on the d-pad";
}
else if(controllers[i].getComponent(identifier).getPollData() == 0.75)
{
texte_manette = "Down on the d-pad";
}
else if(controllers[i].getComponent(identifier).getPollData() == 0.25)
{
texte_manette = "Up on the d-pad";
}
else if(controllers[i].getComponent(identifier).getPollData() == 0.5)
{
texte_manette = "Right on the d-pad";
}
else
{
stick_dead_zone = false;
}
}
else if(comp.getName().equals("Z Axis"))
{
if(value > 0.5)
{
texte_manette = "Left trigger";
}
else if(value < -0.5)
{
texte_manette = "Right trigger";
}
else
{
stick_dead_zone = false;
}
}
else
{
buffer.append(comp.getName()).append(" changed to ");
}
/*
* Check the type of the component and display an
* appropriate value
*/
if (comp.isAnalog()) {
buffer.append(value);
}
else {
if (value == 1.0f) {
buffer.append("On");
}
else {
buffer.append("Off");
}
}
if(stick_dead_zone)
{
//finir_loop = false;
if(texte_manette != null)
{
selection.effacer_le_input(controllers[i].getName(), texte_manette, i);
}
else
{
selection.effacer_le_input(controllers[i].getName(), comp.getName(), i);
}
le_timer.stop();
return;
}
}
}
}
}
};
le_timer = new Timer(10, actionlistener);
le_timer.start();
}
}
I’m a bit at a loss at this point. I’m using Eclipse for my project and I’m using Windows 8.