Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Developer
Posts: 8
Registered: ‎01-04-2010
My Device: Not Specified
Accepted Solution

key press listener and display

Hi, I am trying to do a sample application where when I click on the "N" key the next image appears and when I click on the "P" Key the previous image re-appears. I want to display 10 photos. I tried using the KeypadListener interface but it doesn't work for me. Is this the right method? Can you advice what the best way is to click on a key and have an image appear? thanks DK

Developer
Posts: 19,623
Registered: ‎07-14-2008
My Device: Not Specified

Re: key press listener and display

I presume you are using a MainScreen to display these images.  If so, just override keyChar and forget the keyListener. 

New Developer
Posts: 8
Registered: ‎01-04-2010
My Device: Not Specified

Re: key press listener and display

Hi,

I am trying to do a similar implementation where I can display the letter that I have typed in a picture form. Below is the code I have implemented but it does not work. Can you advise me on where I have gone wrong?

 

 

import net.rim.device.api.system.KeyListener;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.MainScreen;

public class abc extends UiApplication implements KeyListener {

   
Scr mScreen;

   
public abc() {
        mScreen
= new Scr();
        pushScreen
(mScreen);
        addKeyListener
(this);
   
}

   
public static void main(String[] args) {
        abc abcGame
= new abc();
        abcGame
.enterEventDispatcher();
   
}

   
private void updateScreen(final String text) {
        mScreen
.addLine(text);
   
}

   
public boolean keyChar(char key, int status, int time) {
        updateScreen
("keyChar " + key);
       
return true;
   
}

   
public boolean keyDown(int keycode, int time) {
       
return false;
   
}

   
public boolean keyRepeat(int keycode, int time) {
       
return false;
   
}

   
public boolean keyStatus(int keycode, int time) {
       
return false;
   
}

   
public boolean keyUp(int keycode, int time) {
       
return false;
   
}
}

class Scr extends MainScreen {
   
int letter;
   
LabelField abcLabel = new LabelField("The Letter : "
               
+ String.valueOf(letter));

   
public Scr() {
       
super(VERTICAL_SCROLL | VERTICAL_SCROLLBAR);
        add
(abcLabel);
   
}

   
public void addLine(final String text) {
        getApplication
().invokeLater(new Runnable() {
               
public void run() {
                        abcLabel
.setText("The Letter : "
                                       
+ String.valueOf(letter));
                        insert
(new LabelField(text), 1);
               
}
       
});
   
}


}
Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: key press listener and display

1. get rid of the key listener implementation (remove everything after "main" in the app class)

 

2. override keyChar() in your MainScreen class

 

3. get rid of the "invoke later", since you will be calling the addLine() method from within the keychar() method, which is in the event thread

 

You might also gain some valuable insight by working through some of the simple examplea that ship with the JDE.

 

 

 

 

 

 

Developer
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified

Re: key press listener and display

try something like this:

 

  public boolean keyChar(char key, int status, int time) {

if(key == Characters.N)
{getNextImage();}
else if(key == Characters.P){getPreviousImage();}

       
return true;
   
}


public void getNextImage()
{ // write your implementation. }

public void getPreviousImage()
{ // write your implementation. }
Thanks and Regards,
PraveenGoparaju.
New Developer
Posts: 8
Registered: ‎01-04-2010
My Device: Not Specified

Re: key press listener and display

Hi,

 

Thank you for your quick reply. I tried the solutions mentioned above Characters.N does not work for me.. This is what I eventually did :

 

 

package com.blackberry.abc;

import net.rim.device.api.system.KeyListener;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.Menu;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.component.BitmapField;

public class abc extends UiApplication {

    Scr mScreen;

    public abc() {
        mScreen = new Scr();
        pushScreen(mScreen);

    }

    public static void main(String[] args) {
        abc abcGame = new abc();
        abcGame.enterEventDispatcher();
    }



}

class Scr extends MainScreen {
    
    LabelField abcLetter = new LabelField("");
    LabelField LetterA = new LabelField("a");
    BitmapField bitmapField;
    BitmapField bitmapFieldabc;
    Bitmap abcBitmap = Bitmap.getBitmapResource("");

    
    public boolean keyChar(char key, int status, int time) {
        addLine("" + key);
        return true;
    }


    public Scr() {
        super(VERTICAL_SCROLL | VERTICAL_SCROLLBAR);
        Bitmap logoBitmap = Bitmap.getBitmapResource("Welcome.gif");
        bitmapField = new BitmapField(logoBitmap, Field.FIELD_HCENTER);
        add(bitmapField);       
        add(abcLetter);      
                          
        bitmapFieldabc = new BitmapField(abcBitmap, Field.FIELD_HCENTER);
        add(bitmapFieldabc);                                                                 
        }

    public void addLine(final String text) {
              
                        abcLetter.setText(""
                                        + text);
                                        if (text == "a"){
        abcBitmap = Bitmap.getBitmapResource("Image1.jpg");
        bitmapFieldabc.setBitmap(abcBitmap);
                                 
                      }             
                                   
    }
   
}

 

The welcome jpg appears but when I click on "a" - A.jpg does not appear. Please let me know where I am going wrong.

 

Thanks

DK



 

Developer
Posts: 19,623
Registered: ‎07-14-2008
My Device: Not Specified

Re: key press listener and display

[ Edited ]

I presume you have debugged this and proved that the code you expect to be executed is?

 

Anyway, you need to tell the screen that it needs to repaint the Bitmap.  Not tested but I think after

 

bitmapFieldabc.setBitmap(abcBitmap);

 

you should be able to just

 

bitmapFieldabc.invlidate();

 

or perhaps invalidate() the screen.

 

 Also, a belated Welcome, sorry didn't realise this was your first post!

New Developer
Posts: 8
Registered: ‎01-04-2010
My Device: Not Specified

Re: key press listener and display

Hi ,

 

Thank you for the support given to solve the above issue. I have managed to resolve it with your input.

 

Now I am trying to attach sound effects for each image. This is what my code looks like :

 

 

  if (text.equals("apple")){
         try{
       abcBitmap = Bitmap.getBitmapResource("Apple.jpg");
       InputStream isA = getClass().getResourceAsStream("/Apple.mp3");
       player = javax.microedition.media.Manager.createPlayer(isA, "audio/mpeg");
         player.realize();
         player.prefetch();
         player.start();            // Start the player
         Manager.playTone(0x45, 2000, 100);
         player.getDuration();
         Thread.sleep(1000);
         } catch (Exception e) {
                    e.printStackTrace();
                }
                                              
       }
         else if (text.equals("ball")){
               try{
       abcBitmap = Bitmap.getBitmapResource("Ball.jpg");
              InputStream isB = getClass().getResourceAsStream("/Ball.mp3");
       player = javax.microedition.media.Manager.createPlayer(isB, "audio/mpeg");
         player.realize();
         player.prefetch();
         player.start();            // Start the player
         Manager.playTone(0x45, 2000, 100);
        } catch (Exception e) {
                    e.printStackTrace();
                }
                                              
       } 

 

Even though the sound effects do come I cannot hear it everytime I press the key. I am not sure if this is because of the size of my mp3 file. and sometimes if the word for one image is heard the word for the other image is not heard. Can you guide me as to where I am going wrong and what is my best option?

 

Developer
Posts: 19,623
Registered: ‎07-14-2008
My Device: Not Specified

Re: key press listener and display

This is a different problem.

 

Can I suggest you mark this problem as Solved and start a new problem with your question and a Subject that is more accurate.

 

I think you will find there are similar posts on playing sounds already one the forum and I'm sure there their a KB articles about it.

 

AND, you should not call sleep on the Event Thread.  This is dangerous.  So think about why you are calling sleep and what is actually happening here, in terms of the Threads used, especially the Thread that runs the Player.

 

To repeat, solve this Thread, start a new one with your question, but before you do that, review the KB and the forum for approaches to playing sounds, and make sure you are not calling sleep (or any other blocking function) on the Event Thread.

Developer
Posts: 216
Registered: ‎02-18-2010
My Device: 9350,8520
My Carrier: no

Re: key press listener and display

 

Please use, 

ByteArrayInputStream is = (ByteArrayInputStream)this.getClass().getResourceAsStream("/Apple.mp3");

insead of

InputStream is=getClass().getResourceAsStream(/Apple.mp3);


Because in case of player taking byte array increase the speed of application. Please try it.