Hi shanu14 and welcome to Nokia's Discussion Boards,
A sample code for what you need is this:
The CanvasKeyPressed MIDlet:
Code:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class CanvasKeyPressed extends MIDlet implements CommandListener{
CanvasKey canvas;
private Display display;
Command cmd2=new Command("KeyPress2",Command.OK,0);
public void startApp(){
canvas = new CanvasKey();
display = Display.getDisplay(this);
display.setCurrent(canvas);
canvas.addCommand(cmd2);
canvas.setCommandListener(this);
}
public void pauseApp(){}
public void destroyApp(boolean unconditional){}
public void commandAction(Command c, Displayable d)
{
if(c==cmd2)
{
canvas.keyPressed(2);
}
}
}
The CanvasKey supporting java class:
Code:
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
class CanvasKey extends Canvas{
private Font font;
private String message = "[PRESS KEY]";
public CanvasKey(){
font = Font.getFont(Font.FACE_PROPORTIONAL,
Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
}
public void mypress(int akey)
{
keyPressed(akey);
}
public void paint(Graphics g){
int width = getWidth();
int height = getHeight();
g.setColor(255, 0, 0);
g.fillRect(0, 0, width - 1, height - 1);
g.setColor(0, 0, 255);
g.drawRect(0, 0, width - 1, height - 1);
g.setFont(font);
int x = width / 2;
int y = height / 2;
g.drawString(message, x, y, Graphics.BASELINE | Graphics.HCENTER);
}
protected void keyPressed(int keyCode){
message="KeyCode is "+keyCode;
repaint();
}
protected void pointerPressed(int x, int y)
{
message="Pressed!";
repaint();
}
protected void pointerReleased(int x, int y)
{
message="Released!";
repaint();
}
protected void pointerDragged(int x, int y)
{
message="Dragged!";
repaint();
}
}
Make sure that you also check
a) the Java ME examples in the wiki:
http://www.developer.nokia.com/Commu...tegory:Java_ME
b) the Touch UI articles in Java Developer's Library:
http://library.developer.nokia.com/t...B166C077F.html
c) the examples in the Library (including the multipoint touch event examples)
http://library.developer.nokia.com/t...33A115191.html