Hi thanks for showing interest on my problem .Here is the code that i have modified for kuixcanvas
/*
* This file is part of org.kalmeo.kuix.
*
* org.kalmeo.kuix is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* org.kalmeo.kuix is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with org.kalmeo.kuix. If not, see <http://www.gnu.org/licenses/>.
*
* Creation date : 21 nov. 2007
* Copyright (c) Kalmeo 2007-2008. All rights reserved.
* http://www.kalmeo.org
*/
package org.kalmeo.kuix.core;
import java.io.IOException;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.GameCanvas;
import org.kalmeo.kuix.core.focus.FocusManager;
import org.kalmeo.kuix.transition.Transition;
import org.kalmeo.kuix.util.Metrics;
import org.kalmeo.kuix.widget.Desktop;
import org.kalmeo.util.NumberUtil;
import org.kalmeo.util.StringTokenizer;
import org.kalmeo.util.worker.Worker;
import org.kalmeo.util.worker.WorkerTask;
//added by venky
iimport net.rim.device.api.system.KeyListener;
/**
* This is the Kuix implementation of the J2ME {@link GameCanvas}.
* This canvas intercept all user events (keyboard and pointer)
*
* @author bbeaulant
*/
public final class KuixCanvas extends GameCanvas implements KeyListener {
// Associated KuixInitializer
private final KuixInitializer initializer;
// The root Desktop widget
private Desktop desktop;
// This value is true if the canvas is fully initialized
private boolean initialized = false;
// Workaround to fix the bug when the vm do not call sizeChanged method
private boolean sizeInitialized = false;
// Specify if the KuixCanvas is hidded
private boolean hidded = true;
// The GameCanvas and BuffuredImage graphics instance
private Graphics canvasGraphics;
private Graphics imageBufferGraphics;
// Double buffering image for devices that doesn't implement it natively
private Image imageBuffer = null;
// Transition stuff
private Transition transition;
private boolean transitionRunning = false;
// Last pointer drag status
private int pointerPressedX = 0;
private int pointerPressedY = 0;
private boolean pointerDragged = false;
// KuixCanvas Worker's task properties
private boolean needToChangeSize = false;
private int desiredWidth = -1;
private int desiredHeight = -1;
private Vector keyEvents;
private Vector pointerEvents;
private boolean needToRevalidate = false;
private boolean needToRepaint = false;
private final Metrics repaintRegion = new Metrics();
private WorkerTask workerTask;
// DebugInfos properties
private int debugInfosKuixKeyCode = KuixConstants.KUIX_KEY_STAR;
private byte debugInfosKeyCounter = 0;
private boolean debugInfosEnabled = false;
private long lastFpsTickTime = 0;
/**
* Construct a {@link KuixCanvas}. By default the canvas is auto created by
* the {@link KuixMIDlet}. But if you create it manualy @se
* {@link KuixInitializer}.
*
* @param initializer
* @param fullscreen
*/
public KuixCanvas(KuixInitializer initializer, boolean fullscreen) {
super(false);
if (initializer == null) {
throw new IllegalArgumentException("initializer couldn't be null");
}
this.initializer = initializer;
setFullScreenMode(fullscreen);
// Try to extract the debug infos key from the JAD file
if (initializer.getMIDlet() != null) {
String debugInfosKey = initializer.getMIDlet().getAppProperty(KuixConstants.KUIX_DEBUG_INFOS_KEY_APP_PROPERTY);
if (debugInfosKey != null) {
byte[] shortcuts = KuixConverter.convertShortcuts(debugInfosKey);
if (shortcuts != null) {
setDebugInfosKuixKeyCode(NumberUtil.toInt(shortcuts, 0));
}
}
}
}
/**
* Returns the {@link KuixInitializer} instance.
*
* @return the initializer instance
*/
public KuixInitializer getInitializer() {
return initializer;
}
/**
* Returns the {@link Desktop} instance.
*
* @return the desktop instance.
*/
public Desktop getDesktop() {
return desktop;
}
//added by venky for the keyactions ------------- start
// Invoked when a key has been pressed.
public boolean keyDown(int keycode, int time)
{
//menu keycode is 268566528
if(keycode==268566528)
{
System.out.println("======menu key is recognised====keycode=======keyDown===="+keycode);
// processKeyEvent(KuixConstants.KEY_PRESSED_EVENT_TYPE, 268566528);
processKeyEvent(KuixConstants.KEY_PRESSED_EVENT_TYPE, SOFT_KEY_LEFT_DEFAULT);
}
//escape keycode is 1769472
if(keycode==1769472)
{
processKeyEvent(KuixConstants.KEY_PRESSED_EVENT_TYPE, SOFT_KEY_RIGHT_DEFAULT);
System.out.println("======back key is recognised====keycode=======keyDown===="+keycode);
}
else {
processKeyEvent(KuixConstants.KEY_PRESSED_EVENT_TYPE, keycode);
}
return true;
}
//Invoked when a key has been repeated.
public boolean keyRepeat(int keycode, int time)
{
System.out.println("keycode=======keyRepeat===="+keycode);
return true;
}
//Invoked when the ALT or SHIFT status has changed.
public boolean keyStatus(int keycode, int time)
{
System.out.println("keycode=======keyStatus===="+keycode);
return true;
}
// Invoked when a key has been released.
public boolean keyUp(int keycode, int time)
{
System.out.println("keycode=======keyUp===="+keycode);
return true;
}
// Invoked when a sequence of zero or more keyDowns generates a character.
public boolean keyChar(char key, int status, int time)
{
System.out.println("keycode=======keyChar===="+key);
return true;
}
//added by venky for the keyactions ------------- end
//public static char net.rim.device.api.ui.KeypadUtil.getKeyChar(int keycode , int mode)
/**
* Define the key that the user need to press 3 consecutive times to display
* on screen debug infos. This value define one or more keys (ex:
* <code>KuixConstants.KUIX_KEY_STAR | KuixConstants.KUIX_KEY_POUND</code>,
* press * or # to activete debug onfos display)<br>
* By default the value si set to <code>KuixConstants.KUIX_KEY_STAR</code>.
*
* @param debugInfosKuixKeyCode the debugInfosKuixKeyCode to set
*/
protected void setDebugInfosKuixKeyCode(int debugInfosKuixKeyCode) {
this.debugInfosKuixKeyCode = debugInfosKuixKeyCode;
}
/**
* @return the debugInfosEnabled
*/
public boolean isDebugInfosEnabled() {
return debugInfosEnabled;
}
/**
* @param debugInfosEnabled the debugInfosEnabled to set
*/
public void setDebugInfosEnabled(boolean debugInfosEnabled) {
this.debugInfosEnabled = debugInfosEnabled;
repaintNextFrame();
}
/**
* Define the next repaint transition. The transition delay depends on the
* transition implementation.
*
* @param transition
*/
public void setTransition(Transition transition) {
this.transition = transition;
transitionRunning = false;
}
/**
* Initialize the Canvas
*/
protected void initialize() {
//added by venky for adding the keylistener interface;;;;;;;;;;
Application.getApplication().addKeyListener(this);
// Try to init statup locale
if (initializer.getMIDlet() != null) {
String locale = initializer.getMIDlet().getAppProperty(KuixConstants.KUIX_LOCALE_APP_PROPERTY);
if (locale != null) {
Kuix.initI18nSupport(locale);
}
}
// Init the desktop styles
initializer.initDesktopStyles();
// Create the desktop
desktop = new Desktop();
desktop.setBounds(0, 0, getWidth(), getHeight());
// Init keyCode adapter
platformName = getPlatform();
softKeyLeft = getLeftSoftkeyCode();
softKeyRight = getRightSoftkeyCode();
softKeyDelete = getDeleteKeyCode();
softKeyBack = getBackKeyCode();
// Add blackberry soft keys emulation commands
/*if (platformName == KuixConstants.PLATFORM_BLACKBERRY) {
final Command softKeyLeftCommand = new Command(Kuix.getMessage(KuixConstants.SOFT_LEFT_I18N_KEY), Command.OK, 1);
final Command softKeyRightCommand = new Command(Kuix.getMessage(KuixConstants.SOFT_RIGHT_I18N_KEY), Command.OK, 2);
addCommand(softKeyLeftCommand);
addCommand(softKeyRightCommand);
setCommandListener(new CommandListener() {
/* (non-Javadoc)
* @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command, javax.microedition.lcdui.Displayable)
public void commandAction(Command c, Displayable d) {
if (c == softKeyLeftCommand) {
processKeyEvent(KuixConstants.KEY_PRESSED_EVENT_TYPE, SOFT_KEY_LEFT_DEFAULT);
return;
}
if (c == softKeyRightCommand) {
processKeyEvent(KuixConstants.KEY_PRESSED_EVENT_TYPE, SOFT_KEY_RIGHT_DEFAULT);
return;
}
}
});
}*/
// Init worker's task
keyEvents = new Vector();
pointerEvents = new Vector();
workerTask = new WorkerTask() {
/* (non-Javadoc)
* @see org.kalmeo.kuix.core.worker.WorkerTask#execute()
*/
public boolean run() {
if (needToChangeSize) {
forceSizeChanged(desiredWidth, desiredHeight);
}
if (sizeInitialized) {
// Key events, Pointer events and revalidation are execute only if transition is not running
if (!transitionRunning) {
// Key events
if (!keyEvents.isEmpty()) {
synchronized (this) {
for (int i = 0; i < keyEvents.size(); ++i) {
int[] keyEvent = ((int[]) keyEvents.elementAt(i));
FocusManager focusManager = desktop.getCurrentFocusManager();
if (focusManager != null && focusManager.processKeyEvent((byte) keyEvent[0], keyEvent[1])) {
repaintNextFrame();
}
}
}
keyEvents.removeAllElements();
}
// Pointer events
if (!pointerEvents.isEmpty()) {
synchronized (this) {
for (int i = 0; i < pointerEvents.size(); ++i) {
int[] pointerEvent = ((int[]) pointerEvents.elementAt(i));
FocusManager focusManager = desktop.getCurrentFocusManager();
if (focusManager != null && focusManager.processPointerEvent((byte) pointerEvent[0], pointerEvent[1], pointerEvent[2])) {
repaintNextFrame();
} else if ((byte) pointerEvent[0] == KuixConstants.POINTER_DROPPED_EVENT_TYPE) {
if (desktop.getDraggedWidget() != null) {
desktop.removeDraggedWidget(true);
}
}
}
}
pointerEvents.removeAllElements();
}
// Revalidate if needed
if (needToRevalidate) {
forceRevalidate();
}
}
// Repaint
if (needToRepaint) {
forceRepaint();
}
}
return false;
}
};
// Init the desktop content (Populate the desktop for the first run)
initializer.initDesktopContent(desktop);
// Initialization complete
initialized = true;
// Push the canvas worker task and then the first revalidate and repaint will be done
Worker.instance.pushTask(workerTask);
}
/* (non-Javadoc)
* @see javax.microedition.lcdui.game.GameCanvas#paint(javax.microedition.lcdui.Graphics)
*/
public void paint(Graphics g) {
if (!sizeInitialized) {
forceSizeChanged(getWidth(), getHeight());
}
super.paint(g);
if (initialized) {
repaintNextFrame();
} else {
// Draw the initialization splash screen. The duration of this screen depend on device
// Extract custom atributes
String message = initializer.getInitializationMessage();
String imageFile = initializer.getInitializationImageFile();
Image image = null;
// Construct image if exists
if (imageFile != null) {
try {
image = Image.createImage(imageFile);
} catch (IOException ioe) {
}
}
// Compute placement
int textWidth = 0;
int textHeight = 0;
int imageWidth = 0;
int imageHeight = 0;
if (message != null) {
Font font = Font.getDefaultFont();
textWidth = font.stringWidth(message);
textHeight = font.getHeight();
}
if (image != null) {
imageWidth = image.getWidth();
imageHeight = image.getHeight();
}
// Draw background
g.setColor(initializer.getInitializationBackgroundColor());
g.fillRect(0, 0, getWidth(), getHeight());
// Draw message
if (message != null) {
g.setColor(initializer.getInitializationMessageColor());
g.drawString( message,
(getWidth() - textWidth) / 2,
(getHeight() - imageHeight - textHeight) / 2 + imageHeight,
0);
}
// Draw image
if (image != null) {
g.drawImage(image,
(getWidth() - imageWidth) / 2,
(getHeight() - imageHeight - textHeight) / 2,
0);
}
}
}
/* (non-Javadoc)
* @see javax.microedition.lcdui.Canvas#sizeChanged(int, int)
*/
protected void sizeChanged(int w, int h) {
if (sizeInitialized) {
needToChangeSize = true;
desiredWidth = w;
desiredHeight = h;
if (!hidded) {
revalidateNextFrame();
}
} else {
forceSizeChanged(w, h);
}
}
/* (non-Javadoc)
* @see javax.microedition.lcdui.Canvas#hideNotify()
*/
protected void hideNotify() {
hidded = true;
}
/* (non-Javadoc)
* @see javax.microedition.lcdui.Canvas#showNotify()
*/
protected void showNotify() {
hidded = false;
revalidateNextFrame();
}
/**
* Repaint through the Worker task
*/
public void repaintNextFrame() {
repaintNextFrame(0, 0, getWidth(), getHeight());
}
/**
* Repaint through the Worker task
*
* @param x x coordinate of the repaint region
* @param y y coordinate of the repaint region
* @param width width of the repaint region
* @param height height of the repaint region
*/
public void repaintNextFrame(int x, int y, int width, int height) {
needToRepaint = true;
repaintRegion.add(x, y, width, height);
}
/**
* Revalidate the desktop throught the Worker task. <code>repaintNextFrame</code> is automaticaly call.
*/
public void revalidateNextFrame() {
needToRevalidate = true;
repaintNextFrame();
}
/**
* Revalidate (and repaint) the desktop as soon as possible. If the current
* thread is the worker thread the task is done imm?dialty else it is
* deferred to the next frame.
*/
public void revalidateAsSoonAsPossible() {
if (!Worker.instance.isCurrentThread()) {
revalidateNextFrame();
} else {
forceRevalidate();
forceRepaint();
}
}
/**
* Repaint the desktop as soon as possible. If the current thread is the
* worker thread the task is done imm?dialty else it is deferred to the next
* frame.
*/
public void repaintAsSoonAsPossible() {
if (!Worker.instance.isCurrentThread()) {
repaintNextFrame();
} else {
forceRepaint();
}
}
/**
* Resize the {@link KuixCanvas}.
*
* @param w
* @param h
*/
private void forceSizeChanged(int w, int h) {
needToChangeSize = false;
desiredWidth = -1;
desiredHeight = -1;
imageBuffer = Image.createImage(w, h);
imageBufferGraphics = imageBuffer.getGraphics();
canvasGraphics = getGraphics();
if (desktop != null) {
desktop.setBounds(0, 0, w, h);
}
sizeInitialized = true;
}
/**
* Force desktop to be revalidated
*/
private void forceRevalidate() {
desktop.revalidate();
needToRevalidate = false;
}
/**
* Force desktop to be repaint
*/
private void forceRepaint() {
// Define clip rect region
boolean repaintRegionOnly = !repaintRegion.isEmpty();
if (repaintRegionOnly) {
imageBufferGraphics.setClip(repaintRegion.x, repaintRegion.y, repaintRegion.width, repaintRegion.height);
}
if (transition != null) {
if (!transitionRunning) {
Image oldImage = Im