1 Reply Latest reply: Nov 19, 2008 5:54 AM by 843851 RSS

    problem using UserEvents and KeyEvents

    843851
      For some design probles I have to implemet an xlet that use
      org.dvb.event.UserEvent
      and
      java.awt.event.KeyEvent
      Below there is a sample. The xlet start listening to UserEvents, then if you press RED it listen to KeyEvents and if you press GREEN it should listen to UserEvents again.
      I tryed the code on real boxes but when I get back to UserEvents and press for example 10 the STB does not tune to that service. Has any of you an Idea how to do it? or can you explain me the reason?
      import java.awt.event.KeyEvent;
      import java.awt.event.KeyListener;
      
      import javax.tv.xlet.Xlet;
      import javax.tv.xlet.XletContext;
      import javax.tv.xlet.XletStateChangeException;
      
      import org.dvb.event.EventManager;
      import org.dvb.event.UserEvent;
      import org.dvb.event.UserEventListener;
      import org.dvb.event.UserEventRepository;
      import org.havi.ui.HScene;
      import org.havi.ui.HSceneFactory;
      import org.havi.ui.HScreen;
      import org.havi.ui.event.HRcEvent;
      
      /**
       * Event Xlet
       * 
       */
      public class EventXlet implements Xlet, KeyListener, UserEventListener {
           
           private static final int[] INTRO_KEYS = { HRcEvent.VK_COLORED_KEY_0,
                HRcEvent.VK_ENTER };
           
           private XletContext context;
           
           private HScene scene;
           
           private UserEventRepository repository;
           
           public EventXlet() {
           }
           
           public void initXlet(XletContext xletContext)
           throws XletStateChangeException {
                context = xletContext;
           }
           
           public void startXlet() throws XletStateChangeException {
                HSceneFactory hsceneFactory = HSceneFactory.getInstance();
                scene = hsceneFactory.getFullScreenScene(HScreen.getDefaultHScreen()
                          .getDefaultHGraphicsDevice());
                scene.setSize(720, 576);
                scene.setLayout(null);
                scene.setVisible(true);
                
                // I want to listen to UserEvents.
                addUserEventsListener();
                System.out.println("==\nApplication started");
           }
           
           
           private void addUserEventsListener() {
                
                repository = new UserEventRepository("myRep");
                
                for (int i = 0; i < INTRO_KEYS.length; i++) {
                     repository.addKey(INTRO_KEYS);
                }
                
                EventManager.getInstance().addUserEventListener(this, repository);
                
           }
           
           private void removeUserEventsListener() {
                EventManager.getInstance().removeUserEventListener(this);
                
                for (int i = 0; i < INTRO_KEYS.length; i++) {
                     repository.removeKey(INTRO_KEYS[i]);
                }
           }
           
           private void removeKeyEventsListener() {
                scene.removeKeyListener(this);
                
           }
           
           private void addKeyEventsListener() {
                scene.requestFocus();
                scene.addKeyListener(this);
                
           }
           
           public void keyPressed(KeyEvent event) {
                System.out.println("==\nreceiced a KeyEvent");
                if (event.getKeyCode() == HRcEvent.VK_COLORED_KEY_1) {
                     System.out.println("==\nchanging to user events");
                     removeKeyEventsListener();
                     addUserEventsListener();
                }
           }
           
           public void userEventReceived(UserEvent event) {
                if (event.getType() == HRcEvent.KEY_PRESSED) {
                     System.out.println("==\nreceiced an UserEvent");
                     if (event.getCode() == HRcEvent.VK_COLORED_KEY_0
                               || event.getCode() == HRcEvent.VK_ENTER) {
                          System.out.println("==\nchanging to key events");
                          removeUserEventsListener();
                          addKeyEventsListener();
                     }
                }
           }
           
           public void pauseXlet() {
           }
           
           public void destroyXlet(boolean flag) throws XletStateChangeException {
                
           }
           
           public void keyTyped(KeyEvent e) {
           }
           
           public void keyReleased(KeyEvent e) {
           }
      }