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: 9
Registered: ‎04-09-2009
My Device: Not Specified

How to get the correct keycode in Canvas.keyPressed() in a MIDlet?

[ Edited ]

I am trying to get the keyCode from Canvas.KeyPressed in MIDlet. It seems when enable Multitap, I can't type "B.

My steps:

1. enable Multitap

2. press "shift"

3. press KEY_NUM2 continous twice in short time.

 => At this point, I got "A" and "a", instead of "B".

 

I wrote a simple MIDlet looks like:

 =================================================================


public class TouchScreenMIDlet extends MIDlet{
 
 public void destroyApp(boolean unconditional)
   throws MIDletStateChangeException {
  // TODO Auto-generated method stub

 }

 public void pauseApp() {
  // TODO Auto-generated method stub

 }

 public void startApp() throws MIDletStateChangeException {
  
  Display.getDisplay(this).setCurrent(new TouchScreenCanvus());
 }

}

================================================================

 

public class TouchScreenCanvus extends BlackBerryCanvas{

 TouchScreenCanvus(){
  super();
 }
 
 public void paint(Graphics g) {
  g.fillRect(0, 0, 10, 10);

 }
 
 public void keyPressed(int keyCode){
  
        System.out.println("TouchScreenCanvus keyPressed: "+keyCode);
 }

}

 

 =================================================================

 

With eclipse plugin emulator(v4.7.0.46), the keyCode prointed is 65 and 97, while I am expecting 66. 

If I lock the "shift" key, I can get A, and then B, and then C. But how can I get the correct keyCode without forcing user locking the shift key and yet still get B?

Message Edited by Cecelia on 04-09-2009 10:07 AM
BlackBerry Development Advisor
Posts: 15,070
Registered: ‎07-09-2008
My Device: BlackBerry Passport
My Carrier: Bell

Re: How to get the correct keycode in Canvas.keyPressed() in a MIDlet?

This is the expected behaviour.  When capturing keys presses, the first character on the key (the left most character) is passed in.  It is up to the application to handle input for additional letters on the same key.  You could detect when the user presses the key twice in rapid succession and substitute for the second character.

Mark Sohm
BlackBerry Development Advisor

Please refrain from posting new questions in solved threads.
Problem solved? Click the Accept As Solution button.
Found a bug? Report it using Issue Tracker