J2ME Canvas KeyPressed

This tutorial is going to show you how to handle keypressed event in J2ME using canvas class. After going through the given example, you will be able to show different output against different keypressed actions.

J2ME Canvas KeyPressed


This tutorial is going to show you how to handle keypressed event in J2ME using canvas class. After going through the given example, you will be able to show different output against different keypressed actions. This example illustrates you how to set different game actions on the various keypressed codes that will help you to show different actions on different key clicks.

 Find out these game Action  used in this example within switch statement:

  • case UP:   message = "[UP]";   break;   
  • case DOWN:   message = "[DOWN]";   break;   
  • case LEFT:   message = "[LEFT]";   break;   
  • case RIGHT:   message = "[RIGHT]";   break;   
  • case FIRE:   message = "[FIRE]";   break;   
  • case GAME_A:   message = "[LEFT_UP]";   break;   
  • case GAME_B:   message = "[RIGHT_UP]";   break;   
  • case GAME_C:   message = "[LEFT_DOWN]";   break;   
  • case GAME_D:   message = "[RIGHT_DOWN]";   break;   
  • default:   message = "";   break;

And the following methods is used to paint the screen and text:

  • 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);

The Application is as follows:


Source Code of KeyCanvas.java

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class KeyCanvas extends MIDlet{

  private Display display;

  public void startApp(){
  Canvas canvas = new CanvasKey();
  display = Display.getDisplay(this);

  public void pauseApp(){}

  public void destroyApp(boolean unconditional){}

class CanvasKey extends Canvas{

  private Font font;
  private String message = "[PRESS KEY]";

  public CanvasKey(){
  font = Font.getFont(Font.FACE_PROPORTIONAL, 


  public void paint(Graphics g){
  int width = getWidth();
  int height = getHeight();

  g.fillRect(00, width - 1, height - 1);
  g.drawRect(00, width - 1, height - 1);

  int x = width / 2;
  int y = height / 2;

  g.drawString(message, x, y, Graphics.BASELINE | Graphics.HCENTER);

  protected void keyPressed(int keyCode){
  int gameAction = getGameAction(keyCode);

  case UP:
  message = "[UP]";
  case DOWN:
  message = "[DOWN]";
  case LEFT:
  message = "[LEFT]";
  case RIGHT:
  message = "[RIGHT]";
  case FIRE:
  message = "[FIRE]";
  case GAME_A:
  message = "[LEFT_UP]";
  case GAME_B:
  message = "[RIGHT_UP]";
  case GAME_C:
  message = "[LEFT_DOWN]";
  case GAME_D:
  message = "[RIGHT_DOWN]";
  message = "";

Download Source Code

Share on Google+Share on Google+

J2ME Canvas KeyPressed

Posted on: November 13, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles



Discuss: J2ME Canvas KeyPressed   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
March 20, 2011
how to handle Display.GAME_UP message in j2me

i m using htmlcomponent... i overridded the keyPressed menthod and handled the msgs Dislay.GAME_LEFT,Dislay.GAME_RIGHT but due to this my scrollbar in htmlComponent is not working what to do? how to move scrollbar down on Dislay.GAME_DOWN message
June 1, 2012
keypressed() in j2me

i'm having a problem in using the keypressed() function.i want if i pressed a key(KEY_NUM1),an integer a increment.this integer is displayed on a label. here are my codes: import javax.microedition.midlet.*; import javax.microedition.lcdui.Canvas; import javax.microedition.lcdui.Graphics; import com.sun.lwuit.*; import com.sun.lwuit.events.*; import com.sun.lwuit.layouts.BorderLayout; import com.sun.lwuit.layouts.GridLayout; import com.sun.lwuit.Container; import com.sun.lwuit.Form; import com.sun.lwuit.Label; import com.sun.lwuit.Command; import com.sun.lwuit.events.ActionListener; import com.sun.lwuit.animations.CommonTransitions; import com.sun.lwuit.animations.Transition3D; import com.sun.lwuit.util.Resources; import com.sun.lwuit.plaf.UIManager; public class TESTMidlet extends MIDlet implements ActionListener { private Form Main, Main2 ,f; int a = 0; int b = 0; // static int c = 0; // static int d = 0; // static int e = 0; // static int g = 0; // static int h = 0; // static int i = 0; // static int j = 0; // static int k = 0; // static int l = 0; // static int m = 0; // static int n = 0; // static int o = 0; public void startApp() { Display.init(this); try { Resources r = Resources.open("/a.res"); UIManager.getInstance().setThemeProps(r.getTheme(r.getThemeResourceNames()[0])); } catch (java.io.IOException ioe) { // Do something here. } f= new Form("instruction!!!!"); f.addCommand(new Command("BACK",2)); f.setCommandListener(this); f.setLayout(new BorderLayout()); Container formContainer3 = new Container(new BorderLayout()); Label instructionLabel = new Label("instuctions will be here"); formContainer3.addComponent(BorderLayout.CENTER, instructionLabel); f.addComponent(BorderLayout.CENTER, formContainer3); f.show(); //SECOND FORM Main2 = new Form("MOBSCOB VERSION 1"); Main2.addCommand(new Command("BACK",2)); Main2.setCommandListener(this); Main2.setLayout(new GridLayout(8,3)); Label home1 = new Label("Home"); Label home2 = new Label(); Label home3 = new Label("Away"); Label home4 = new Label(""); home4.setText(Integer.toString(a)); Label home5 = new Label("Q1"); Label home6 = new Label(""); home6.setText(Integer.toString(b)); Label home7 = new Label(""); home7.setText(Integer.toString(a)); Label home8 = new Label("Q2"); Label home9 = new Label(""); home9.setText(Integer.toString(b)); Label home10 = new Label(""); home10.setText(Integer.toString(a)); Label home11 = new Label("Q3"); Label home12 = new Label(""); home12.setText(Integer.toString(b)); Label home13 = new Label(""); home13.setText(Integer.toString(a)); Label home14 = new Label("Q4"); Label home15 = new Label(""); home15.setText(Integer.toString(b)); Label home16 = new Label(""); home16.setText(Integer.toString(a)); Label home17 = new Label("OT1"); Label home18 = new Label(""); home18.setText(Integer.toString(b)); Label home19 = new Label(""); home19.setText(Integer.toString(a)); Label home20 = new Label("OT2"); Label home21 = new Label(""); home21.setText(Integer.toString(b)); Label home22 = new Label(""); home22.setText(Integer.toString(a)); Label home23 = new Label("FS"); Label home24 = new Label(""); home24.setText(Integer.toString(b)); Main2.addComponent( home1); Main2.addComponent( home2); Main2.addComponent( home3); Main2.addComponent( home4); Main2.addComponent( home5); Main2.addComponent( home6); Main2.addComponent( home7); Main2.addComponent( home8); Main2.addComponent( home9); Main2.addComponent( home10); Main2.addComponent( home11); Main2.addComponent( home12); Main2.addComponent( home13); Main2.addComponent( home14); Main2.addComponent( home15); Main2.addComponent( home16); Main2.addComponent( home17); Main2.addComponent( home18); Main2.addComponent( home19); Main2.addComponent( home20); Main2.addComponent( home21); Main2.addComponent( home22); Main2.addComponent( home23); Main2.addComponent( home24); Main2.show(); //COMMANDS FOR FORM2 Command QUATER1_CMD = new Command("Q1",6); Command QUATER2_CMD = new Command("Q2",4); Command QUATER3_CMD = new Command("Q3",5); Command QUATER4_CMD = new Command("Q4"); Command OT1_CMD = new Command("OT1"); Command OT2_CMD = new Command("OT2"); Main2.addCommand(OT2_CMD); Main2.addCommand(OT1_CMD); Main2.addCommand(QUATER4_CMD); Main2.addCommand(QUATER3_CMD); Main2.addCommand(QUATER2_CMD); Main2.addCommand(QUATER1_CMD); Main2.addCommandListener(this); Main2.setTransitionOutAnimator( CommonTransitions.createSlide( CommonTransitions.SLIDE_HORIZONTAL, false, 500)); //FIRST FORM Main = new Form("MOBSCOB VERSION 1"); Main.show(); Main.addCommand(new Command("Exit", 0)); Main.setCommandListener(this); Main.setLayout(new BorderLayout()); Container formContainer = new Container(new BorderLayout()); Label welcome = new Label("Welcome to MOBSCOB version 1 "); formContainer.addComponent(BorderLayout.CENTER, welcome); Main.addComponent(BorderLayout.CENTER, formContainer); Command PLAY_CMD = new Command("BEGIN",1); Command INSTRUCTIONS_CMD = new Command("INSTRUCTIONS",3); Main.addCommand(INSTRUCTIONS_CMD); Main.addCommand(PLAY_CMD); Main.addCommandListener(this); Button button = new Button("Push me!"); Main.setTransitionOutAnimator(Transition3D.createRotation(500, true)); //exercise on output } public void pauseApp(){} public void destroyApp(boolean unconditional) {} public void actionPerformed(ActionEvent ae) { Command cmd = ae.getCommand(); switch (cmd.getId()) { //'Exit' command case 0: notifyDestroyed(); break; case 1: Main2.show(); break; case 2: Main.show(); break; case 3: f.show(); break; case 6: Main2.show(); break; } } class canvas extends Canvas implements Runnable{ public void keypressed(int keyCode){ int key = getKeyCode(keyCode); if(key == KEY_NUM1) { a=a+1; } } protected void paint() { } public void run() { } protected void paint(Graphics g) { throw new UnsupportedOperationException("Not supported yet."); } } } i need some help!!!thank you!!!
July 9, 2012
this canvas game programs

this canvas game programs - It return errors