Test if the button were pushed in joystick

i want When I press a specific button in joystick
an action will be occurs
Such as writing the name of the button in TextField

i use this code but did not work ?

 private void check(){
    JInputJoystick joystick = new JInputJoystick(Controller.Type.STICK);
    
     int selectedControllerIndex = getSelectedControllerName();
            Controller controller = foundControllers.get(selectedControllerIndex);
    while (joystick.isControllerConnected()) {
        // Go trough all components of the controller.
        Component[] components = controller.getComponents();
        
        System.out.print(Arrays.toString(components));
        
        for (Component component : components) {
            Identifier componentIdentifier = component.getIdentifier();

            // Buttons
            if(componentIdentifier.getName().matches("^[0-9]*$")){ // If the component identifier name contains only numbers, then this is a button.
                // Is button pressed?
                if(component.getPollData() == 0.0f) {
                    System.out.print("Button got pressed!");
                }
            }
        }
      
    }
}

Have you read the ‘Getting started’ sticky post in the jinput section?

You seem to have missed the part about having to poll each device. If you read the settings but never poll then the data will always read 0.

HTH

Endolf

What exactly do you mean when you say this code “did not work”? Have you tried debugging, or at least adding some print statements, to figure out when the program’s execution differs from your expectations?

Does it enter that function? Does it enter that while loop? Does it enter that for loop? Does it enter that if statement? What about that last if statement? What are the values of every variable when the code does something you don’t expect?

ok i see it
and I’ve put code in a method and i call method in constructor
But output like this

http://im86.gulfup.com/uGqz2G.png

here the method i Used

 private void poll(){
       //Controller[] controllers = ControllerEnvironment.getDefaultEnvironment().getControllers();
     
    
       Controller[] controllers = ControllerEnvironment.getDefaultEnvironment().getControllers();
             Controller firstMouse=null;

      for(int i=0;i<controllers.length && firstMouse==null;i++) {
          if(controllers[i].getType()==Controller.Type.MOUSE) {
            // Found a mouse
            firstMouse = controllers[i];
         }
      }
      if(firstMouse==null) {
         // Couldn't find a mouse
         System.out.println("Found no mouse");
         System.exit(0);
      }
      
      System.out.println("First mouse is: " + firstMouse.getName());      
      while(true) {
              firstMouse.poll();
         Component[] components = firstMouse.getComponents();
         StringBuilder buffer = new StringBuilder();
         for(int i=0;i<components.length;i++) {
            if(i>0) {
               buffer.append(", ");
            }
            buffer.append(components[i].getName());
            buffer.append(": ");
                if(components[i].isAnalog()) {
               buffer.append(components[i].getPollData());
            } else {
               if(components[i].getPollData()==1.0f) {
                  buffer.append("On");
               } else {
                  buffer.append("Off");
               }
            }
         }
         System.out.println(buffer.toString());
         
         try {
            Thread.sleep(20);
         } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
         }
      }
   
   
   }

Try running the controller read test, you might find you have more than one mouse, especially if you are on a laptop.

You can always print the device name as the first part of your print line.

Endolf