DirectAndRawInputEnvironmentPlugin not recognizing events or giving polldata

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.

Hi

Which version of JInput do you use? Which controller do you use for your tests? I rarely use JInput but the last time I had to detect if a controller was plugged or not, I just had to use a listener, it was very simple and portable. You have to force the scan in the legacy version, otherwise it occurs only once in the default environment.

I just switched to a newer version of Jinput and I’m still not getting any results. I currently tried with a USB keyboard, but I used various controllers before (X360, PS3 Afterflow, Mad Catz stick). Like I said, everything was working perfectly fine until I decided to use DirectAndRawInputEnvironmentPlugin() which is supposed to be checking the controllers plugged in at all time. It does seem to detect the controllers and keyboards, but I’m not getting any response from any button pressed afterward.

EDIT:
Like suggested in some other post, I changed DirectAndRawInputEnvironmentPlugin for a new constructor instead of DefaultEnvironment. The good news is that it does detect the controllers and keyboard being plugged or unplugged. But for some reasons, it doesn’t want to detect any keyboard input. Seems a bit odd that it would detect my X360 controller, but not my keyboard input at all. I saw on some posts that sometimes you need to change the focus, would that be the reason that I always get a polldata of 0 and no event detection from the keyboard input ?

private static ControllerEnvironment createDefaultEnvironment() throws ReflectiveOperationException {

	    // Find constructor (class is package private, so we can't access it directly)
	    Constructor<ControllerEnvironment> constructor = (Constructor<ControllerEnvironment>)
	        Class.forName("net.java.games.input.DefaultControllerEnvironment").getDeclaredConstructors()[0];

	    // Constructor is package private, so we have to deactivate access control checks
	    constructor.setAccessible(true);

	    // Create object with default constructor
	    return constructor.newInstance();
	}
	
	static {

	    AccessController.doPrivileged(new PrivilegedAction<Object>() {
	        public Object run() {
	            String os = System.getProperty("os.name", "").trim();
	            if (os.startsWith("Windows 8")) {  // 8, 8.1 etc.

	                // disable default plugin lookup
	                System.setProperty("jinput.useDefaultPlugin", "false");

	                // set to same as windows 7 (tested for windows 8 and 8.1)
	                System.setProperty("net.java.games.input.plugins", "net.java.games.input.DirectAndRawInputEnvironmentPlugin");

	            }
	            return null;
	        }
	    });

	}

Do you use this code in an AWT application?

Yes I do use various JFrame and JPanel for the interface. At the moment of the keyboard input, there is a Jpanel being displayed on the entire screen (to indicate that the User must press a button). This JPanel is set to visible false normally, but upon starting this function, the JPanel is set to visible true. I also tried plugging different keyboards (in case mine could be broken), but still no results. I tried changing the focus with requestFocus() and requestFocusInWindow() by setting it up on the main JFrame, but that didn’t work either.

EDIT, finally found the solution:

The main problem is new DirectAndRawInputEnvironmentPlugin() which is deleting all the event queue for a keyboard when you create a new one (even if you create a second variable). For now, the solution I found was to create a new variable with a timer to avoid re-creating the DirectAndRawInputEnvironmentPlugin() variable to often and it seems to work. Also, I used poll() to detect if a controller is unplugged, when a controller is unplugged, I re-created the DirectAndRawInputEnvironmentPlugin() variable. Feel free to use my code for those in the future that would need to solve this problem:

import javax.swing.JFrame;
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.Identifier;


public class detect_all_bouttons extends JFrame{
	
	boutton_control_selection selection;
	Timer le_timer;
	Controller[] controllers;
	Controller[] controllers_temp;
	creation_frame fenetre_principale;
	DirectAndRawInputEnvironmentPlugin directEnv = new DirectAndRawInputEnvironmentPlugin();
	int le_temps = 0;
	boolean la_reponse = true;
	
	public detect_all_bouttons (boutton_control_selection la_section, creation_frame la_fenetre){
		selection = la_section;
		Boolean finir_loop = true;
		fenetre_principale = la_fenetre;
		
		ActionListener actionlistener= new ActionListener()
		{ 
		    public void actionPerformed(ActionEvent e) 
		    {   
		    	if(controllers == null)
		    	{
		    			controllers = directEnv.getControllers();
		    	}

		    		if(la_reponse == false || le_temps == 10)
		    		{
		    			directEnv = new DirectAndRawInputEnvironmentPlugin();
		    			controllers = directEnv.getControllers();
		    			le_temps = 0;
			    	}
		    		
		    		le_temps++;
		    		la_reponse = true;
		    	
				StringBuffer buffer = null;
				
				for (int i = 0; i < controllers.length; i++) {

					if(controllers[i].getType()!=Controller.Type.MOUSE)
					{
						
							EventQueue queue = controllers[i].getEventQueue();
						/* Create an event object for the underlying plugin to populate */
							Event event = new Event();
						
						/* For each object in the queue */
							if(controllers[i].poll() == true)
							{
								controllers[i].poll();
									while (queue.getNextEvent(event)) {

											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;
											}
									}
							}
							else
							{
								System.out.println(controllers[i].getName());
								la_reponse = false;	
							}
						}
						
					}
		    	}
		}; 
		le_timer = new Timer(100, actionlistener);
		le_timer.start();
	
	}
}