Button listener callbacks not fired

This is where you talk about the EV3 software itself, installation issues, and programming talk.

Moderators: roger, gloomyandy, skoehler

Button listener callbacks not fired

Postby jlafaye » Sat Jan 04, 2014 6:41 pm

Hello everybody,

I just bought an EV3 retail kit and trying to use lejos 0.5.0-alpha. I am a professional c++/python programmer and bought the EV3 to entertain & play with my children. I expected the installation of lejos to be painful but it worked quite flawlessly (only had to patch wpa_supplicant.conf manually).

I am having a hard time making button listeners work. I have reduced my program to the listing below. I expect this basic program to display the key pressed (either DOWN, UP or CENTER) and exit when the ESCAPE button is pressed. Pressing the ESCAPE button works (the program exits) but the listener callback is not fired when the other buttons are pressed. I am probably missing something obvious but I have been trying to find the solution for more than one hour and I think it's time to request for help to the experimented guys.

Code: Select all
import lejos.hardware.Button;
import lejos.hardware.ButtonListener;
import lejos.hardware.lcd.LCD;


public class EV3FirstProgram {


    public static void main(String[] args) {
        LCD.clear();
        LCD.drawString("First EV3 Program", 0, 5);

      ButtonListener buttonListener = new ButtonListener() {

         @Override
         public void buttonPressed(Button b) {
            LCD.clear();
            if (b == Button.DOWN) {
               LCD.drawString("DOWN pressed", 0, 0);
               return;
            }
            if (b == Button.UP) {
               LCD.drawString("UP pressed", 0, 0);
               return;
            }
            if (b == Button.ENTER) {
               LCD.drawString("CENTER pressed", 0, 0);
               return;
            }
            if (b == Button.ESCAPE) {
               LCD.drawString("ESCAPED pressed", 0, 0);
               System.exit(0);
            }
            
            // emit a bip
            LCD.drawString("unknown button pressed", 0, 0);
         }

         @Override
         public void buttonReleased(Button b) {
            // TODO Auto-generated method stub
            
         }
         
      };
      Button.DOWN  .addButtonListener(buttonListener);
      Button.UP    .addButtonListener(buttonListener);
      Button.ENTER .addButtonListener(buttonListener);
      
        Button.ESCAPE.waitForPressAndRelease();
    }
   
}
jlafaye
New User
 
Posts: 2
Joined: Sat Jan 04, 2014 6:31 pm

Re: Button listener callbacks not fired

Postby gloomyandy » Sat Jan 04, 2014 7:30 pm

As always a look at the source tells the story...
https://sourceforge.net/p/lejos/ev3/ci/ ... utton.java

//TODO: Sort out listeners or get rid of them!


Button listeners are not currently supported on the EV3. We are slowly working our way through most of the older APIs and reworking them. The Button interface is still on that list. Many of the leJOS team do not like the listener model (it has many potential issues and tends to introduce race conditions and other problems). Use of them for Buttons seems slightly less of a problem than for some other things so we may keep them in some form or other. If you really want them it would not be hard to implement them yourself for now...

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3967
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: Button listener callbacks not fired

Postby jlafaye » Sat Jan 04, 2014 8:13 pm

Thank you for your answer !
jlafaye
New User
 
Posts: 2
Joined: Sat Jan 04, 2014 6:31 pm


Return to EV3 Software

Who is online

Users browsing this forum: No registered users and 2 guests

more stuff