Thursday, May 6, 2010

J2ME : Getting softkey events in Canvas in MIDlet

Sometimes it gets an irritating experience when a MIDlet does not take keyEvents for softkeys.

Here is how to get the same :
i)Your canvas should not have any commands & should no implement a Command Listener.
ii)You should make the canvas in fullscreen mode before doing anything else.

Here Is The Code :
import javax.microedition.lcdui.*;
public class SoftK extends Canvas
{
//Constructor
public SoftK()
{
 this.setFullScreenMode(true);
//Other code here
}
public void paint(Graphics g)
{
//paint code here
}
public void keyPressed(int keyCode)
{
if(keyCode==-7)
{
//Right SoftKey
}
if(keyCode==-6)
{
//LeftSoftKey
}
}
}

No comments:

Post a Comment